During the time of the Judges when there was a famine, an Israelite family from Bethlehem – Elimelech, his wife Naomi, and their sons Mahlon and Chilion – emigrated to the nearby country of Moab. Elimelech died, and the sons married two Moabite women: Mahlon married Ruth and Chilion married Orpah.
After about ten years, the two sons of Naomi also died in Moab (1:4). Naomi decided to return to Bethlehem. She told her daughters-in-law to return to their own mothers and remarry. Orpah reluctantly left; however, Ruth said, "Intreat me not to leave thee, [or] to return from following after thee: for whither thou goest, I will go; and where thou lodgest, I will lodge: thy people [shall be] my people, and thy God my God: Where thou diest, will I die, and there will I be buried: the LORD do so to me, and more also, [if aught] but death part thee and me." (Ruth 1:16–17 KJV)
The two women returned to Bethlehem at the beginning of the barley harvest, and in order to support her mother-in-law and herself, Ruth went to the fields to glean. As it happened, the field she went to belonged to a man named Boaz, who was kind to her because he had heard of her loyalty to her mother-in-law. Ruth told Naomi of Boaz's kindness, and she gleaned in his field through the remainder of barley and wheat harvest.
Boaz was a close relative of Naomi's husband's family. He was therefore obliged by the Levirate law to marry Mahlon's widow, Ruth, in order to carry on his family's inheritance. Naomi sent Ruth to the threshing floor at night and told her to go where he slept, and "uncover his feet, and lay thee down; and he will tell thee what thou shalt do." (3:4) Ruth did so. At midnight Boaz was afraid and turned to see that a woman lay at his feet. When asked who she was, she replied: "I [am] Ruth thine handmaid: spread therefore thy skirt over thine handmaid; for thou [art] a near kinsman." (3:9) Boaz blessed her and agreed to do all that is required, and he noted that, "all the city of my people doth know that thou [art] a virtuous woman." (3:11) He then acknowledged that he was a close relative, but that there was one who was closer, so he deferred to spread his cloak over her at that time, and she remained in submission at his feet until she returned into the city in the morning. Early that day, Boaz discussed the issue with the other male relative, Ploni Almoni ("so-and-so"), before the town elders. The other male relative was unwilling to jeopardize the inheritance of his own estate by marrying Ruth, and so relinquished his right of redemption, thus allowing Boaz to marry Ruth. They transferred the property and redeemed it by the nearer kinsman taking off his shoe and handing it over to Boaz. (Ruth 4:7–18) Boaz and Ruth were married and had a son named Obed: who is "the father of Jesse, the father of David." (4:13–17)

Question: Why was a near relative obliged to marry to widow of a dead kinsman?
Answer: To carry on family inheritence
Alternative answer: By the Levirate law