The “Narada Purana” contains the story connected to origin of Guruvayur shrine : King Janamejaya, in order to avenge the death of his father Parikshit, brought about by the Naga chief Takshaka, performed the “Sarpayajna” (snake sacrifice) in which thousands of innocent reptiles perished. As a result of their curse that befell him, he was afflicted with leprosy, which left him in utter despair. Sensing this, sage Dattatreya appeared before him with a remedy Beg for mercy of Lord Krishna at Guruvayur.
Janamejaya asked about the glorious of the lord and the whereabouts of the place. In Reply, the sage recounted how at the beginning of the Padma Kalpa, Lord Vishnu gave this deity to Brahma,who could execute his task of Creation by worshipping the lord in his deity form.
At the beginning of the Varaha Kalpa, the childless couple, Sutapa & Prishni prayed in earnest to Brahma for the gift of a son. Brahma gave them this deity assuring them of their wish if they worshipped the deity with sincere devotion. This they did with excellence, so much so that the Lord Vishnu himself appeared before them. As a boon, they prayed Him three times, for a son. He pacified them that he himself would be born to them as their son in three successive births. Moreover, they would also have the good fortune of worshipping the divine deity in all the three births and so..as the Lord wished, in the 1st life, to Sutapa and Prishni was born as their son, Prisnigarbha. In their 2nd life, as Kasyapa&Aditi, the couple worshipped the same deity and got their son Vaman. In their 3rd and most auspicious life as Vasudeva and Devaki, Sri Krishna was born to them.
After slaying Kamsa , Krishna moved to Dwaraka where he built a temple for his parents to worship this deity. When the time come of him to end his earthly pastimes, Lord Krishna entrusted his devoted friend Uddhava, to take good care of the Deity. At the end of his earthly sojourn, Krishna prophesied to his friend and devotee, Uddhava that the island of Dwaraka, which had been his stronghold, would be swept away by the sea, seven days after he would leave. He instructed him to rescue the precious Deity of Vishnu which his parents had worshipped, and hand it over to Brihaspati, the guru of the gods who would come to him……
To know more, visit – http://holydham.com/origin-of-the-shrine-at-guruvayur/