The legend of Vaidyanathan goes something like this. The demon lord from the Ramayan, the devout Ravana meditated hard in order to invoke Lord Shiva, and requested him to come over to Sri Lanka, in order that his capital may become invincible.
It is said that he attempted to lift the holy mountain, Lord Shiva's abode, Mount Kailash and take it with him to his capital; however Shiva crushed him with his finger, and Ravana prayed to him and sought his mercy, after which Shiva gave him one of the twelve Jyotirlingams with the condition that if it was placed on the ground it would take root immediately and hence Ravana had to carry it back on his trek to his capital. Varuna the God of water, entered Ravana's belly, and caused him to feel the need to relieve himself.Vishnu then came down in the form of a lad and volunteered to hold the Jyotirlingam as he relieved himself. But before Ravana could return, the young lad placed the jyotirlingam on the ground to which it became rooted. A disappointed Ravana offered severe penances to Shiva here, and cut off nine of his heads as a part of his repentance. Shiva revived him and joined the heads to the body, like a Vaidya or a physician; hence this Jyotirlingam goes by the name Vaidyanath. The same legend holds at Gokarnam in Karnataka.