King Harishchandra lived in the Treta Yuga. He was an honest, noble and just king. His subjects enjoyed prosperity and peace. He had a queen named Shaivya (also called Taramati) and a son named Rohitashva. Once, while on a hunting expedition, he heard the cries of a woman asking for help. Armed with a bow and arrow, he went in the direction of the sound. The sound was an illusion created by Vighnaraja, the lord of the obstacles. Vighnaraja was trying to disturb the tapasya (meditation) of the sage Vishwamitra.
When he saw Harishchandra, he entered the king's body and started abusing Vishwamitra. This disturbed Vishwamitra's tapasya, and destroyed all the knowledge that the sage had acquired during this tapasya.
When Harishchandra came to his senses, he realized that the sage was extremely angry with him, and apologized. He promised to fulfill any of the sage's desires to get rid of his guilt. Vishwamitra demanded dakshina (donation) for his rajasuya yajna. The king asked him what he wanted in donation. In response, Vishwamitra said 'Give me all that you have except yourself, your wife and your child.'
Harishchandra agreed to the demand. He let go of all his possessions - even his clothes. As he readied to leave his palace with his family, Vishwamitra demanded another donation. Harishchandra said that he did not have any possession left, but promised to make another donation within a month.
Harishchandra started living in penury with his wife and his family. His loyal subjects followed him. When Vishwamitra saw the king with his subjects, he started cursing Harishchandra for taking along his subjects (who were a part of the kingdom donated to the sage). The king then decided to leave the kingdom with his family.
To make them go away sooner, Vishwamitra started beating the queen with a stick. When the five guardians of the directions saw this, they condemned Vishwamitra. The sage cursed them to take birth as human beings. These guardian deities were born as the sons of the Pandavas and Draupadi.
Nearly a month after leaving his kingdom, Harishchandra arrived in the holy city of Kashi, only to see Vishwamitra already present there. The sage demanded the donation that the king had promised him. Harishchandra pointed out that there was still some time left in completion of one month.
The sage agreed to come back at the next sunset and departed. As his hungry son cried for food, Harishchandra worried how would he be able to make a donation to the sage. His wife Shaivya suggested that he sell her to get some money. After some hesitation, Harishchandra accepted the proposal and sold her to an elderly man. Their child would not let go off his mother, so it was decided that he would accompany his mother.
Soon after, Vishwamitra appeared again and demanded the donation. Harishchandra gave him all the money he had received from the sale of his wife. However, Vishwamitra was unhappy with the donation, and demanded more. Harishchandra then decided to sell himself.
An outcaste chandala (actually the deity of dharma in disguise) offered to buy him, but Harishchandra's self-respect as a high-caste Kshatriya would not allow this. He instead offered to be Vishwamitra's slave. Vishwamitra agreed, but then declared 'Since you are my slave, you must obey me. I sell you to this chandala in exchange of gold coins.' The chandala paid the sage, and took along Harishchandra as a slave.
The chandala employed Harishchandra as a worker at his cremation ground. He directed Harishchandra to collect fees for every body cremated there: a part of the fee would go to the chandala, a part would be given to the local king, and the rest would be Harishchandra's remuneration. Harishchandra started living and working at the cremation ground.
One day, he dreamed about his past lives, and realized that his current condition was a result of his past sins. During this nightmare, he also saw his queen crying before him. When he woke up, he saw his queen actually crying before him. She held the dead body of their son, who had died of a snake bite. Thinking of his misfortune, Harishchandra thought of committing suicide, but then realized that he would have to continue paying for his sins in his next life.
Meanwhile, the queen readied to cremate the dead body of their son. But, Harishchandra told her that he would not let her do so without paying the fee. At that time, all the deities appeared led by the deity of Dharma and accompanied by Vishwamitra. They praised Harishchandra for his good qualities, and invited him to heaven.
After his ascension to heaven, Vashistha – the sage of Harishchandra's royal dynasty – ended his rigourous penance of 12 years. He came to know about the unfortunate events that had happened to Harishchandra during these years. He started a severe fight with Vishwamitra, but was ultimately pacified by Brahma. Brahma explained to him that Vishwamitra was only testing the king, and had actually helped him ascend to the heaven.