I believe that they were both included to show that Jesus is descended from King David by whichever parent you choose. However, that is my own opinion and YMMV.
Joseph was of the Royal line through Solomon. The Royal line had a blood curse, (Jeconiah or Coniah,) that no of his seed would sit on the throne of David. Marys line is not the Royal line and does not have the blood curse. Jesus is Josephs first son through adoption, and can claim the throne of David while he is also a descendant of David through Mary.
So you’re saying Joseph married his COUSIN!!?? (Well, second cousin, 200 times removed or something.)