In the Mahaprasthanika Parva of the Mahabharata, the epic’s second to last chapter, there is a narration of events surrounding the final pastimes of the Pandavas and their ascent into the Himalayas, where they would, one by one, depart from this world.
In Mausala Parva we find the story of Arjuna, who was in a disturbed state due to being unable to save the elders, women and children of the Yadava clan. He reached Hastinapur in a state of great disappointment. Arjuna related to Yudhistira the fate of the Yadava men and the mausala — iron clubs grown from a miraculously originating powder — with which they beat one another to death at the Prabasha sea. The departures of Sri Krsna and Balarama, the submersion of Dwaraka into the Sea… all these things weighed heavily on the Pandavas.
In a state of dejection, Yudhistira, Arjuna, Bhima, Nakula, Sahadeva and Draupadi lost interest in the world and determined that the time of their own departure from this world was at hand. Coronating Pariksit as the King of Hastinapur, Yudhistira was free to take his leave, and led the group on their final journey towards the Himalayas.
The Pandavas stopped to pray at many holy tirthas across the land before ascending the Himalaya foothills. At one point, Lord Agni appeared before the Pandavas and asked Arjuna to return his bow, which had been given to him by Varuna for destroying the asuras on Earth. Arjuna returned the bow, and Agni immediately disappeared from view. In Rishikesh, the Pandavas stopped to offer prayers to Lord Shiva and installed a Shivalingam there.
Along the way they were joined by a dog, who accompanied them on the remainder of their journey. As the Pandavas travelled onward, Draupadi was the first among them to drop and die. Bhima asked Yudhistira the reason for her untimely death, before she could arrive in Heaven in her human form. He replied that although all five of the brothers loved her equally, her affections had been more inclined towards Arjuna. It was this partiality that caused her to depart midway along the journey.
Sahadeva was the next to fall, and Bhima again asked Yudhistira about the reason for his death. Yudhistira replied that Sahadeva was very proud, because no one was his equal in wisdom. For this reason, he departed. Nakula was the next of the brothers to leave his body, and the reason Yudhistira gave was that being the most handsome person in the world, his pride had overcome him, resulting in his departure midway through the journey.
Yudhistira then instructed Arjuna and Bhima to not look back, and to continue their journey with determination. Arjuna was the next Pandava brother to die, and this left Bhima very astonished. Again Bhima inquired from Yudhistira why Arjuna had not reached the heavens in human form. His elder brother replied that Arjuna had been boastful, vowing at the start of the Kurukshetra war that he would destroy all the Kauravas in a single day. Although very proud of his archery skills he was unable to fulfill this vow, thus the war raged on for eighteen days. Consequently, Arjuna had to be left behind by the remaining two brothers.
Yudhistira, Bhima and the Dog proceeded on, but eventually Bhima himself fell. Inquiring again from his senior brother, Yudhistira replied that Bhima had eaten too much in his lifetime, without considering the hunger of others. For this reason, he was destined to die at this stage of the journey. Yudhistira and the Dog thus continued on alone, journeying towards the Heavens with their mortal bodies intact.
The identity of the canine on this divine journey is disclosed at the very end of the story:
Having lost his beloved Draupadi and all his brothers along the way, with only his companion the Dog remaining by his side, one day Lord Indra appeared in his chariot before Yudhistira. Because Yudhistira was the most pious of the brothers and had never strayed from the path of Dharma, Indra wished to now take him directly to the heavens in his human form. At first, Yudhistira refused to enter the chariot without Draupadi and his brothers, but Indra assured him that they were already in the spiritual world, and he would again meet them there.
Yudhistira then asked to take the Dog along in the chariot, but Indra strongly objected. Saying that because food is taken sitting on the floor, it is not possible to have a dog roaming in the same place. Indra also indicated that the Dog would defile Heaven, and it is considered that the mere glance of a dog deprives deity offerings of their consecration.
Yudhistira, however was adamant: he would not go without the Dog, who had been entirely devoted to him. It was the Dog who had remained loyal at the time of his great losses of Draupadi and each brother. Saying he could not be happy in Heaven, because he would be haunted by thoughts of the Dog who had remained so loyal to him, Yudhistira refused Indra’s offer.
Soon after, the Dog transformed into Dharma, the God of righteousness. This had been the final test for Yudhistira, who to the very end, never swayed from the path of Dharma. He thus enjoyed the rare honour of reaching heaven in the human form.