Jesus was the son of David, and since Jesus was born from Mary (the virgin birth), she must have been from David also.
The book of the generation of Jesus Christ, the son of David, the son of Abraham.
- Matthew 1:1 (KJV)
But Matthew and Luke provide different genealogies for Joseph, and none (at first sight) are given for Mary.
In Matthew Joseph is from Jacob:
And Jacob begat Joseph the husband of Mary, of whom was born Jesus, who is called Christ.
- Matthew 1:16 (KJV)
But in Luke Joseph is from Heli:
And Jesus himself began to be about thirty years of age, being (as was supposed) the son of Joseph, which was the son of Heli.
- Luke 3:23 (KJV)
This seems like a contradiction at first, but once we understand that the genealogy in Matthew is of Joseph, and that the genealogy in Luke is of Mary things will make a lot more sense.
The key to understanding this, is that genealogies are never given as "daughter of ...". It's always the son that is mentioned. To understand Luke, we must understand that "son" here means "son-in-law". So Jacob begot Joseph, who married Mary (who was from Heli).
Note also that it says "as was supposed". That's a clear indication that Joseph was not the birth father of Jesus.
This means that the genealogy in Luke is that of Mary.
Really interesting is that both Joseph and Mary are from the David bloodline. This means that Jesus (through Mary) was a son of David in the literal sense, and (through Joseph) was also legally a son of David.
The concept of these two different genealogies may seem hard to understand, but 2,000 years ago this would have made perfect sense.
It's probably best not to get too caught up in it.
But avoid foolish disputes, genealogies, contentions, and strivings about the law; for they are unprofitable and useless.
- Titus 3:9 (KJV)