This incident takes place in the Vana Parva of the Mahabharata when Pandavas had gone for vanvas and were at the end of their eleventh year. After bidding adieu to Ghatotkacha and his followers, they next repaired to the monarch of mountains in the vicinity of the Yamuna. In the midst of the mountain abounding in waterfalls and having grey and orange coloured slopes and summits covered with a shell of snow, forest of Visakhayupa like unto the forest of Chitraratha and inhabited by wild boars and various kinds of deer and birds, made it their home. Dependent on hunting as their chief occupation, the sons of Pritha peacefully dwelt in that forest for one year. There in a cavern of the mountain, Bhima, with a heart afflicted with distraction and grief, came across a snake of huge strength, distressed with hunger and looking fierce like death itself. At this crisis Yudhishthira, the best of pious men, became the protector of Bhima, also known as Vrikodar, and he, of infinite puissance, extricated Bhima whose whole body had been fast gripped by the snake with its folds.
Yudhishthira, finding his brother, Bhimsen, coiled by the body of the serpent, said these words: “O son of Kunti, how hast thou come by this misfortune! And who is this best of serpents having a body like unto a mountain mass?”
Bhimasen said, “O worshipful one, this mighty being hath caught me for food! He is the royal sage Nahusha living in the form of a serpent.”
Yudhishthira said, “O long lived one, do thou free my brother of immeasurable prowess, we will give thee some other food, which will appease thy hunger.”
The serpent said, “I have got for diet this son of a king, who came to my mouth of himself. Do thou go away. Thou shouldst not stay here. (If thou remainest here) thou too shall be my fare tomorrow. O mighty armed one, this is ordained in respect of me, that he that cometh unto my place, becometh my food and thou too art in my quarter. After a long time I have got thy younger brother as my food; I will not let him off; neither do I like to have any other food.”
Yudhishthira said, “O serpent, whether thou art a god, or a demon, or an Uraga, do thou tell me truly, it is Yudhishthira that asketh thee, wherefore, O snake, hast thou taken Bhimasena? By obtaining which, or by knowing what will thou receive satisfaction, O snake, and what food shall I give thee? And how mayst thou free him?”
The serpent said, “O sinless one, I was thy ancestor, the son of Ayu and fifth in descent from the Moon. And I was a king celebrated under the name of Nahusha. And by sacrifices and asceticism and study of the Vedas and self-restraint and prowess I had acquired a permanent dominion over the three worlds. And when I had obtained such dominion, haughtiness possessed me. And thousands of Brahmanas were engaged in carrying my chair. And intoxicated by supremacy, I insulted those Brahmanas. And, O lord of the earth, by Agastya have I been reduced to this pass! Yet, O Pandav, to this day the memory (of my former birth) hath not forsaken me! And, O king, even by the favour of that high-souled Agastya, during the sixth division of the day, have I got for a meal thy younger brother. Neither will I set him free, nor do I wish for any other food. But if today thou answerest the questions put by me, then, I shall deliver Vrikodara.”
At this Yudhishthira said, “O serpent, ask whatever thou listest! I shall, if I can, answer thy questions with the view of gratifying thee, O snake! Thou knowest fully what should be known by Brahmanas. Therefore, O king of snakes, hearing (thee) I shall answer thy queries !”
The serpent said, “O Yudhishthira, say who is a Brahmana and what should be known? By thy speech I infer thee to be highly intelligent.”
Yudhishthira said, “O foremost of serpents, it is asserted by the wise, in whom are seen truth, charity, forgiveness, good conduct, benevolence, observance of the rites of his order and mercy is a Brahmana. And, O serpent, that which should be known is even the supreme Brahma, in which is neither happiness nor misery and attaining which beings are not affected with misery; what is thy opinion?”
The serpent said, “O Yudhishthira, truth, charity, forgiveness, benevolence, benignity, kindness and the Veda, which worketh the benefit of the four orders, which is the authority in matters of religion and which is true, are seen even in the Sudra. As regards the object to be known and which thou allegest is without both happiness and misery, I do not see any such that is devoid of these.”
Yudhishthira said, “Those characteristics that are present in a Sudra, do not exist in a Brahmana; nor do those that are in a Brahmana exist in a Sudra. And a Sudra is not a Sudra by birth alone nor a Brahmana is Brahmana by birth alone. He, it is said by the wise, in whom are seen those virtues is a Brahmana. And people term him a Sudra in whom those qualities do not exist, even though he be a Brahmana by birth. And again, as for thy assertion that the object to be known (as asserted by me) doth not exist, because nothing exists that is devoid of both (happiness and misery), such indeed is the opinion, O serpent, that nothing exists that is without (them) both. But as in cold, heat doth not exist, nor in heat, cold, so there cannot exist an object in which both (happiness and misery) cannot exist.”
The serpent said, “O king, if thou recognise him as a Brahmana by characteristics, then, O long-lived one, the distinction of caste becometh futile as long as conduct doth not come into play.”
Yudhishthira said, In human society, O mighty and highly intelligent serpent, it is difficult to ascertain one’s caste in this age, because of promiscuous intercourse among the four orders. This is my opinion. Men belonging to all orders (promiscuously) begot offspring upon women of all the orders. And of men, speech, sexual intercourse, birth and death are common. And to this the Rishis have borne testimony by using as the beginning of a sacrifice such expressions as, of what caste soever we may be, we celebrate the sacrifice. Therefore, those that are wise have asserted that character is the chief essential requisite. The natal ceremony of a person is performed before division of the umbilical cord. His mother then acts as Savitri and his father officiates as priest. He is considered as a Sudra as long as he is not initiated in the Vedas. Doubts having arisen on this point, O prince of serpents, Swayambhu Mantu has declared, that the mixed castes are to be regarded as better than the (other) classes, if having gone through the ceremonies of purification, the latter do not conform to the rules of good conduct, O excellent snakes! Whosoever now conforms to the rules of pure and virtuous conduct, him have I, erenow, designated as a Brahmana.”
The serpent replied, “O Yudhishthira, thou art acquainted with all that is fit to be known and having listened to thy words, how can I (now) eat up thy brother Vrikodara?”
Yudhishthira said, “In this world, you are so learned in the Vedas and Vedangas; tell me (then), what one should do to attain salvation?”
The serpent replied, “O scion of the Bharata’s race, my belief is that the man who bestows alms on proper objects, speaks kind words and tells the truth and abstains from doing injury to any creature goes to heaven.”
Yudhishthira enquired, ‘Which, O snake, is the higher of the two, truth or alms-giving? Tell me also the greater or less importance of kind behaviour and of doing injury to no creature.”
The snake replied, “The relative merits of these virtues, truth and alms-giving, kind speech and abstention from injury to any creature, are known (measured) by their objective gravity (utility). Truth is (sometimes) more praiseworthy than some acts of charity; some of the latter again are more commendable than true speech. Similarly, O mighty king, and lord of the earth, abstention from doing injury to any creature is seen to be important than good speech and vice versa. Even so it is, O king, depending on the effects. And now, if thou hast anything else to ask, say it all, I shall enlighten thee!”
Yudhishthira said, “Tell me, O snake, how the incorporeal being’s translation to heaven, its perception by the senses and its enjoyment of the immutable fruits of its actions (here below), can be comprehended.”
The snake replied, ‘By his own acts, man is seen to attain to one of the three conditions of human existence, of heavenly life, or of birth in the lower animal kingdom. Among these, the man who is not slothful, who injures no one and who is endowed with charity and other virtues, goes to heaven, after leaving this world of men. By doing the very contrary, O king, people are again born as men or as lower animals. O my son, it is particularly said in this connection, that the man who is swayed by anger and lust and who is given to avarice and malice falls away from his human state and is born again as a lower animal, and the lower animals too are ordained to be transformed into the human state: and the cow, the horse and other animals are observed to attain to even the divine state. O my son, the sentient being, reaping the fruits of his actions, thus transmigrates through these conditions; but the regenerate and wise man repossess his soul in the everlasting Supreme Spirit. The embodied spirit, enchained by destiny and reaping the fruits of its own actions, thus undergoes birth after birth but he that has lost touch of his actions, is conscious of the immutable destiny of all born beings.”
Yudhishthira asked, “O snake, tell me truly and without confusion how that dissociated spirit becomes cognisant of sound, touch, form, flavour, and taste. O great-minded one, dost thou not perceive them, simultaneously by the senses? Do thou, O best of snakes, answer all these queries!”
The snake replied, “O long-lived one, the thing called Atman (spirit), betaking itself to corporeal tenement and manifesting itself through the organs of sense, becomes duly cognisant of perceptible objects. O prince of Bharata’s race, know that the senses, the mind, and the intellect, assisting the soul in its perception of objects, are called Karanas. O my son, the eternal spirit, going out of its sphere, and aided by the mind, acting through the senses, the receptacles of all perceptions, successively perceives these things (sound, form, flavour, etc.). O most valiant of men, the mind of living creatures is the cause of all perception, and, therefore, it cannot be cognisant of more than one thing at a time. That spirit, O foremost of men, betaking itself to the space between the eyebrows, sends the high and low intellect to different objects. What the Yogis perceive after the action of the intelligent principle, by that is manifested the action of the soul.”
Yudhishthira said, “Tell me the distinguishing characteristics of the mind and the intellect. The knowledge of it is ordained as the chief duty of persons meditating on the Supreme Spirit.”
The snake replied, “Through illusion, the soul becomes subservient to the intellect. The intellect, though known to be subservient to the soul, becomes (then) the director of the latter. The intellect is brought into play by acts of perception; the mind is self-existent. The intellect does not cause the sensation (as of pain, pleasure, etc.), but the mind does. This, my son, is the difference between the mind and the intellect. You too are learned in this matter, what is your opinion ?”
Yudhishthira said, “O most intelligent one, you have fine intelligence and you know all that is fit to be known. Why do you ask me that question? You knew all and you performed such wonderful deeds and you lived in heaven. How could then illusion overpower you? Great is my doubt on this point.”
The snake replied, “Prosperity intoxicates even the wise and valiant men. Those who live in luxury, (soon) lose their reason. So, I too, O Yudhishthira, overpowered by the infatuation of prosperity, have fallen from my high state and having recovered my self-conciousness, am enlightening thee thus! O victorious king, thou hast done me a good turn. By conversing with thy pious self, my painful curse has been expiated. In days of yore, while I used to sojourn in heaven, in a celestial chariot, revelling in my pride, I did not think of anything else, I used to exact tribute from Brahmarshis, Devas, Yakshas, Gandharvas, Rakshasas, Pannagas and all other dwellers of the three worlds. O lord of earth, such was the spell of my eyes, that on whatever creature, I fixed them, I instantly destroyed his power. Thousands of Brahmarshis used to draw my chariot. The delinquency, O king, was the cause of my fall from my high prosperity. Among them, Agastya was one day drawing my conveyance, and my feet came in contact with his body; Agastya then pronounced (this curse) on me, in anger, ‘Ruin seize thee, do thou become a snake.’ So, losing my glory, I fell down from that excellent car and while falling, I beheld myself turned into a snake, with head downwards. I thus implored that Brahmarishi, ‘May this curse be extinguished, O adorable one! You ought to forgive one who has been so foolish from infatuation.’ Then he kindly told me this, as I was being hurled down (from heaven), ‘The virtuous king Yudhishthira will save thee from this curse, and when, O king, horrible sin of pride will be extinguished (in thee), thou shalt attain salvation.’ And I was struck with wonder on seeing (this) power of his austere virtues; and therefore, have I questioned thee about the attributes of the Supreme Spirit and of Brahmanas. Truth, charity, self-restraint, penance, abstention from doing injury to any creature, and constancy in virtue, these, O king, and not his race or family connections, are the means by which a man must always secure salvation. May this brother of thine, the mighty Bhimasena, meet with good luck and may happiness abide with thee!”
So saying, that king, Nahusha, quitted his serpentine form, and assuming his celestial shape he went back to Heaven.
Ref: https://www.holybooks.com/wp-content/uploads/Mahabharata-VOL-3.pdf