The lineage was from Joseph, not Mary.
Luke 1:27 27To a virgin espoused to a man whose name was Joseph, of the house of David; and the virgin’s name was Mary.
If you compare the genealogies of Matthew 1 (Josephs) and Luke 3 (Marys) you would understand that both Joseph and Mary were of the house and lineage of David. Mary was the descendant of Davids son Nathan, brother of Solomon. Joseph was from the line of Solomon.