According to St Matthew, when Mary virginally conceived Jesus, she was betrothed to St Joseph, and they were yet living together (Matt 1:18). This happened during the time within the betrothal period which, among the Jews, involved such a strong and true commitment that the engaged couple were referred to as spouses. So strong a commitment indeed, that it could only be annulled by rejection.
From St Matthew’s Gospel, it is clear that the angel appeared to Joseph to explain that Mary has conceived a child by the Holy Spirit (Matt 1:20); and only then did Mary and Joseph marry and live together. The next few passages of this Gospel confirm this: Mary and Joseph share the escape into Egypt, settle later in Nazareth, and afterwards, they find Jesus among the doctors of the Law, in the Temple of Jerusalem (Luke 2:41-45).
Furthermore, when St Luke describes the annunciation, he refers to Mary as “a virgin betrothed to a man named Joseph, of the House of David”. According to the Gospels, then, St Joseph was indeed married to the Most Holy Virgin Mary. That is certainly the only conclusion truly reflected in the historical tradition documented in the Gospels.
Nevertheless, whether this was St Joseph’s first or second marriage, or whether St Joseph was just an old widower who only took care of Mary, can only be part of speculative legends with no historical guarantee of authenticity.
The first mention in these legends is found in the “Proto-gospel according to James”, from the 2nd century. This text tells us that Mary stayed in the Temple since she was three; and when she turned twelve, the priests searched for someone who could take care of her. The priests convened the widowers of the town, and when an extraordinary sign happened to Joseph’s staff – a dove appeared from it – they handed custody of Our Lady to Joseph. According to this legend, Joseph didn’t take Mary as his spouse: when the angel appeared in Joseph’s dreams, he does not say, as he did in Matthew’s gospel, “Do not fear to take Mary your wife”. Instead, the angel only says, “Be not afraid for this maiden” (XIV, 2).
Other later apocrypha, known as the “pseudo-Matthew”, perhaps from the 6th century, elaborates this story accepting that the priests said to Joseph: “to no other can she be joined in marriage” (VIII, 4), although it only refers to St Joseph as Mary’s custodian.
The fact that Mary was indeed betrothed to Joseph is, on the other hand, accepted in various other texts: in the “Book of Mary’s Nativity” – a summary of the “pseudo-Matthew” apocrypha and also in the “Story of Joseph, the carpenter” (IV, 4-5).
This diversity and lack of consensus confirm that there is not enough evidence to say that St Joseph was married before knowing Mary.
It seems more logical to believe that Joseph was a young man when he betrothed to the Most Holy Virgin Mary, and that it was that his only marriage.