In Deut 25:5-6, it starts the lay the foundations of the levirite marriage statute, the purpose of which is to raise up seed in the name of the deceased brother "that his name be not put out of Israel" (KJV). In the Book of Ruth, Ruth calls upon Boaz to fill the role of the go'el, which he does after the nearer of kin refuses, and with that move, redeems the land, marries Ruth, and together they have Obed. Yet in Ruth 4:18-22, and even more distantly done genealogies like Matt 1:5, neither Elimelech or Mahlon are mentioned - their name seems to disappear from Israel. What exactly does Deut 25:6 mean, then, if the children aren't raised in the name of the deceased?
I'm going to speculate that the author wrote Ruth specifically because it ties a story of faithfulness during the later period of Judges, which was filled with unfaithfulness, with the ancestry of David. Thus, the author supplied Boaz in the genealogy at the end as David's biological forefather, even though Mahlon was the legal forefather. This tradition was followed in Matthew, perhaps again because both Boaz and Ruth were famous. Again, this is speculation.