According to the Ramayana, Rama, the seventh avatar of the god Vishnu, prayed to the god Shiva to absolve him of the sin committed during his war against the king Ravana in Sri Lanka. Rama wanted to have a large lingam to worship Shiva. He directed Hanuman, the lieutenant in his army, to bring a lingam from the Himalayas. When Hanuman was delayed in bringing the lingam, rama built a small lingam out of the sand available in the seashore, which is believed to be the lingam in the sanctum.