Oda confirmed in a Q&A that they formed an alliance, and BB greeted Law with "It's been a while hasn't it?" meaning they met in Rocky Port
Already made a post about it. Imo that alliance doesn't mean they fought together against Wang Zhi. This is what he said:
- Teach, Law and Coby ended up there together from coincidence since they had their own motives
- Law heard there was a poneglyph on Hachinosu and was looking for a ship to infiltrate the territory and get more knowledge on it. In the end, he ended in the wrong ship(Rocky port), where the King of an island was and once that ship arrived in Hachinosu, he was forced to confront the pirates which led to his reputation of having stolen 100 pirates hearts, which he used to get the Shichibukai title
- Coby arrived for an unknown motive, but ended up saving the Civilians there, most likely in the chaos of the battle, and as end result ended up known as Marine Hero
- Blackbeard's motive of being there was most likely to topple Wang Zhi and become King of Pirate Island, so when he came, the trouble Law caused in his battle against the pirates, as well as Coby's actions in handling the Civilians protection is probably what helped Blackbeard in a sense since it allowed him to have his way with Wang Zhi, topple him and taking over Hachinosu in his place