Source: Mahabharata - Adi Parva - Background of Vidura's birth
The interesting thing for me in this story is - Maandavya did not get upset with thieves or king or soldiers; because he didn't find anything Adharmic in their deeds. It was all a series of un-intended consequences. But once he noticed the root-cause of the issue, he didn't hesitate to curse Yama Dharmaraja himself. That is the power of Dharma.Once upon a time there was a Brahmin called Maandavya. He is a brave, dharmic and always interested in penance.
Everyday he used to stand at the base of the tree in front of his house and stretch his palms up and do penance quietly. He did penance for a long time in this manner.
One day a group of thieves came that way along with their stolen stuff followed by soldiers. The thieves hid their money near his hermitage and hid in the bushes around that place.
The soldiers asked Maandavya for the whereabouts of the thieves. Mandavya stood there in silence. The the soldiers searched the premises of his hermitage and found the stolen goods along with the thieves. The soldiers took Maandavya along with the thieves to the king.
The king ordered gave the capital punishment to the thieves. Soldiers took Maandavya too along with the thieves and put him on a spear (The person is made to sit on a spear as part of this punishment. The spear goes into the person's body from his bottom to his head giving him a very painful death)
Mandavya did not die even after staying on the spear for a long time. He kept there in silence doing his pranayama. Many seers come there everyday in the form of birds and showed concern for Maandavya. They asked him "Brahman, what sin you have committed to face this trouble?"
Maandavya told them "Whom shall I blame? No one did adharma to me!".
After seeing Maandavya not die even after so many days the soldiers informed the King about this situation {The punishment is to put Maandavya to death, but he would die}. King was surprised to learn this. He consulted his ministers and decided to seek forgiveness from this great sage.
The king apologized to Maandavya saying "honorable sage! Kindly forgive me for the mistake I made out of ignorance or power. Please do not be angry with me. I pray you". Mandavya forgave the king. The king tried to pull the spear out of Maandavya's body. The tip of the spear wouldn't come out of the sages body, so they cut the spear to that point.
Sage Maandavya continued his life with the tip of the spear in his body. For that he was called "Anee Maandavya" (Anee = tip of the spear).
One day Maandavya reached Yama's abode and asked him for the secret behind his sin that caused all this.
Yama informed him that Maandavya put a thorn in a butterfly in his past, and that is why he is paying for his karma (your past karma - good or bad - will come back multifold).
AniMaandavya - Dharmaraja! I agree with you. But tell me at what age I committed that crime?
Yama - You have done that when you were a boy.
Maandavya - Dharmaraaja, per dharma sastras, the boy will not get attached to any sin until he turns 12 years. You gave me big punishment for something i have done before i turned 12. Since you did not perform your duty properly you will born to a sudra-woman and live a human life.
From that point on wards, Dharma Sastras made a correction; the sins start giving karmic results only after 14 years of age.
Thus Yama Dharmaraja came to human life as Vidura.
A repeating theme is that devatas are shit scared of coming into Human lives. Because once entered into Human life, their karma is influenced by senses (ego, Manas, and senses). It can lead to un-intended karma and the un-ending cycle of birth and death.