Matthew and Luke actually give two different genealogies. Matthew give the genealogy of Jesus through Joseph, the legal, though not the physical father of Jesus. Luke, on the other hand, gives the ancestry of Jesus through Mary from whom Jesus was descended physically as to his humanity. This is a beautiful fulfillment of prophecy and actually testifies to the accuracy of the Bible. Through Joseph, Jesus became the legal heir to the throne while at the same time bypassed the curse of Coniah as prophesied in Jeremiah 22:24-30. Both, of course, were in the line of David so that Jesus had a legal right to the throne as the adopted son of Joseph and was at the same time a physical descendent of David through Mary.
The Ryrie Study Bible gives an excellent summary of the issues here:
Although Coniah had seven sons (perhaps adopted; cf. 1 Chron. 3:17), none occupied the throne. So, as far as a continuing dynasty was concerned, Coniah was to be considered “childless.” Although his line of descendants retained the legal throne rights, no physical descendant (no man of his descendants) would ever prosperously reign on the Davidic throne. The genealogy of Matthew traces the descent of Jesus through Solomon and Jeconiah (Heb., Coniah; Matt. 1:12); this is the genealogy of Jesus’ legal father, Joseph. Luke traces Jesus’ physical descent back through Mary and Nathan to David, bypassing Jeconiah’s line and showing accurately the fulfillment of this prophecy of Jeremiah. If Jesus had been born only in the line of Joseph (and thus of Jeconiah), He would not have been qualified to reign on the throne of David in the Millennium. See note on Matt. 1:11.