King Indradyumna was a great devotee of Lord Vishnu. Feeling that the burden of royal duties interfered with the worship of his Lord, he decided to entrust the kingdom to his ministers and retire to the forest. He built a cottage near the Trikuta mountain and remained absorbed in the meditation of Lord Vishnu.
Realizing what had happened, Indradyumna begged for forgiveness. The Sage mitigated the impact of the curse stating that as ‘Elephant Gajendra’ he would continue to remain devotee to Lord Vishnu and one day, Lord's grace and intervention, he'd be liberated.
Actually, Sage Agastya had noticed that the great King, despite the greatness of his good deeds, still has traces of Ahamkara in him. He felt that the king had to be taught the hard way that self is to be renounced and surrendered to the Lord for attaining moksha.
Gajendra ruled over all the other elephants in the herd. On a hot day, he proceeded with his herd to a lake to cool off in its fresh waters. Suddenly a crocodile living in the lake attacked Gajendra and caught him by the leg.
Gajendra tried for a long time to escape from the crocodile's clutches. All his family, relatives and friends gathered around to help him, but in vain. The crocodile wouldn't simply let go. When they realized that ‘death’ had come close to Gajendra, they left him alone. He trumpeted in pain and helplessness and when the last drop of energy was also sapped, Gajendra called to god Vishnu to save him, holding a lotus up in the air as an offering.
The crocodile in its previous birth was a Gandharva king called HuHu. Once Sage Devala came to visit him. The two were taking bath and Sage Devala was offering prayers to Sun God when Huhu pulled the sage's leg for fun. The enraged sage cursed the king to become a crocodile in his next life. The repentant HuHu asked for pardon. The Sage proclaimed that though he cannot reverse the curse, the crocodile would be liberated from the cycle of birth and death only when Lord Vishnu visited the Earth upon invocation by a holy soul.
Symbolically, Gajendra represents the jiva, Huhu the sins, and the muddy water of the lake the Samsara.
Krishna always come to help His devotee when He is called by heart.
Also read story of Gokarn and liberation of Dhundhukari: