Scholars have wrestled with the differences between Matthew’s and Luke’s genealogy since the beginning. Dating back to the fifth century, it has generally been accepted that Matthew traces his genealogy through Joseph, while Luke’s is through Mary. (Luke mentions that Joseph was “the supposed father of Jesus.”) In the first century, legal status depended on the father, and if a man acknowledged his son’s paternity, that would be the end of the matter.
Interestingly, however, this text could be written in three ways: (a) “Jacob the father of Joseph the husband of Mary, of whom Jesus was born, who is called Christ.” (b) “Jacob begot Joseph, to whom the virgin Mary, having been betrothed to him, bore Jesus who is called Christ.” (c) “Jacob begot Joseph. And Joseph, to whom was betrothed the virgin Mary, begot Jesus who is called Christ.” (a) is the most commonly used translation and conforms nicely with Matthew’s understanding of the virgin birth. Yet, (c) is the most likely for a genealogical document.
There are all sorts of ways to explain the differences – some think Matthew’s represents Christ’s royal character, while Luke’s focuses on his priestly role. They do not agree on the name of Joseph’s father and many explanations have been given for that, including an example of levirate marriage (totally unsubstantiated by any evidence). Because of this, each traces his respective lines through different sons of David. Nonetheless, it is obvious that such details were not of concern to either evangelist. They were more determined to show that Jesus is truly a descendant of David, by the direct will of God. Beyond this, the particulars were not that important.
It is also possible that each genealogy was created independently. If these came about early in the formation of Christianity, their main focus was perhaps to verify Jesus’ messiahship. Mary and Joseph were not the issue.