Interesting question… I too noticed these confusing passages, but I didn’t ask myself many questions about them… I saw this post as an opportunity to gain some insight on this on my own and, maybe, to be of some help for you too…
One thing to observe is that the 2 genealogies listed aren’t the same. There are way more differences than just Joseph’s father. In fact, from David to Jesus they’re totally different. In Mathew, we see Jesus’ line of descendants through Solomon, and in Luke through Nathan, both of them sons of David.
One other thing… In Mathew, all people listed there “begot” their sons, except Joseph. So it’s clear that Jesus isn’t the natural son of Joseph. In Luke, the author says that Jesus was – “as supposed” – the son of Joseph, the son of Heli, the son of Matthat… I read in a book that in Greek, “the son of Joseph” is different in writing than the other forms of “the son of Heli, the son of Matthat…”. “The son of Joseph” is written using an undefined article. All other similar expression (“the son of xxx”) are used with an defined article. So many scholars believe that Joseph wasn’t the son of Heli, but his son-in-law. Thus they also believe that this genealogy isn’t Joseph’s, but Mary’s. The reason that Joseph is listed instead of Mary, is that women’s names weren’t usually listed in genealogies. This would solve in a way the problem of differences between Mathew and Luke. Also, if you look further down in Luke 3:31, you can see that Mary was the daughter of David. So Jesus is indeed, the son of David, even though Joseph was just his adoptive father.
Hope to have been helpful…