Srimad Bhagvata Mahapurana: Book 8: Chapter 4: Verses 1-14
Deliverance of the lord of the elephants
Sri Suka resumed: Applauding the aforesaid feat of Lord Sri Hari, the gods, Rsis (seers) and Gandharvas (present there), headed by Brahma (the creator) and Lord Siva, discharged (from the air) a shower of flowers (on the person of the Lord by way of felicitation). Heavenly drums sounded; the Gandharvas (celestial musicians) danced and sang (songs of praise); while Rsis, Caranas (celestial bards) and Siddhas (a class of demigods endowed with mystic powers from their very birth) glorified the supreme Person. That which was (till now) an alligator due to the curse of the sage Devala has been (in his previous incarnation) the foremost of the Gandharvas, Huhu (by name). Redeemed (from that curse by the Lord), it forthwith assumed a really most wonderful form. Bowing with his head (bent low), he hymned the supreme immortal Lord of excellent renown, the Abode of (all) glory, whose (innumerable) virtues and sacred stories (alone) are worth celebrating.
(Thus) favoured by the Lord, he had (all) his sins washed away; and, walking round Him(as a token of reverence) from left to right and greeting Him, he returned to his home (the realm of the Gandharvas) while the (whole) assembly (of gods and seers present there) stood gazing (with wonder). Freed for ever from the bondage of ignorance through the touch of the Lord, the leader of (the herd of) elephants (in his turn) attained a form similar to the Lord's, being clad in yellow and endowed with four arms. Indeed the elephant was in its previous birth a Pandya king, known by the name of Indradyumna, the foremost of the people of the Dravida country (now known by the name of Tamilnadu), and devoted to the worship of Lord Visnu. The monarch, who had turned an ascetic, wearing matted locks on his head, and having his hermitage on the Kulacala mountain, nay, who had taken a vow of silence and subjugated his mind (too) once proceeded to worship the almighty and immortal Lord Sri Hari with due ceremony at the hour appointed for devotions, having finished his bath (beforehand). By chance there arrived on the spot the sage Agastya of surpassing glory, accompanied by hosts of (his) pupils. Finding him mute (lacking even in polite words) and waiting upon the Lord in seclusion, and perceiving that the king had neglected to offer (him) water to wash his hands with and other articles of worship (to which he was entitled as a venerable newcomer), the sage grew indignant as the story goes. On Indradyumna he pronounced the following imprecation: "This impious, evil-minded and uncultured fellow has insulted a Brahmana (in me) at this moment. Let him (therefore) be steeped in blinding ignorance; and since he is dull-minded like an elephant, let him be born as an elephant."
Sri Suka continued: Having cursed Indradyumna as aforesaid, the glorious Agastya went his way along with his followers, O Pariksit ! Taking the curse to be a freak of (his own) destiny, the royal,sage Indradyumna too entered the, womb of an elephant, that obliterates the consciousness of the Self. It was by virtue of (his) adoration of Sri Hari that God-remembrance was awakened in him at the right moment even in the body of an elephant.Having thus liberated once for all the leader of the herd of elephants and accompanied even by that elephant--(immediately) transformed into the state of a divine attendant-and taking His seat on (the back of) Garuda (the king of birds), Lord Sri Hari (who has a lotus sprung from His navel) withdrew to His transcendent realm (Vaikuntha), His feat (viz., the deliverance of the elephant) being celebrated by Gandharvas, Siddhas and gods. The glory of Sri Krsna (Lord Sri Hari), O great king, has (thus) been narrated to you by me in the form of the aforementioned deliverance of the king of elephants, (the story of) which leads to heaven, is conducive to renown, wipes out the impurities of the Kali age and eliminates (the possibility of) bad dreams in the case of those who listen to this story. O chief of the Kurus !