The asura princess Mahishi was burning up with anger at the trick the gods had pulled on her brother, the asura king Mahishasura. As Mahishasura was blessed with invulnerability to all men, the gods had sent goddess Durga, to fight and kill him. Thus, Mahishi began performing a fearsome set of austerities, and pleased the creator god Brahma. He granted her the boon of ruling the universe and being invulnerable except to a being that had the combined strength of both Shiva and Vishnu. Since such a person did not exist, she thought she was safe and began conquering and plundering the world.
The gods implored Shiva and Vishnu to save them from this catastrophe. Vishnu found a possible solution to the problem. When Vishnu had taken on the Kurma Avatar, he also had to manifest himself as Mohini, the enchantress, to save the nectar of immortality from the demons who were not willing to share it with the gods. If he became Mohini again, then the female Mohini and the male Shiva could have the divine child who would combine their powers and beat Mahishi.
Some versions give a sightly more detailed version of the union of Shiva with Vishnu. One version tells that the asura Basmasura had so pleased lord Shiva with his austerities that Shiva gave him a boon of anything he wished. So Basmasura asked for the ability to burn to ashes anything which he placed his hand over. No sooner had Shiva granted this, than Basmasura ran after the god, threatening to turn him to ashes.
Shiva called to lord Vishnu for help. He hid himself in a certain tree as Basmasura ran here and there searching for the god. Vishnu became aware of the events, and decided that he would take the female form Mohini, “the Enchanting”, and try to trump the asura’s powers. When Basmasura saw Vishnu in this form, he was bewitched by her beauty. He earnestly tried to court her. So Vishnu instructed Basmasura to hold his hand over his head, and vow fidelity. With this act, Basmasura was reduced to ashes.
Vishnu found Shiva and explained the whole affair to him. Shiva asked if he too could see Vishnu in this female form. When Vishnu appeared thus, Shiva was overcome with passion, and united with “her” (Shiva’s seed caught in Mohini’s hands, per one version of the story). The two gods thus became “HariHara Murthy”, that is a composite form of Shiva and Vishnu as one god.
From this union lord Ayyappan was born. He combines in himself the powers of Vishnu and Shiva, and is a visible embodiment of their essential identity. Lord Vishnu gifted the new-born deity with a little bejeweled bell necklace, so this god is called Manikanthan Swamy. The Tamils call him Ayyanar, and he is also called Shastha or Shasthappan by most South Indian communities.
In most Tamil versions of the story, the legend ends with the birth of the god, and with his passage around the region. But in Kerala, the story continues with Ayyappan’s adoption by the Pandalam Raja, and the subsequent encounter with Mahisi.