As per the legend, Ravana the demon king of Sri Lanka prayed fervently to Lord Shiva to make his country Sri Lanka unconquerable. He even lifted Mount Kailash to bring it to the capital. This enraged Lord Shiva who crushed the Mount Kailash with his finger.
Ravana begged for mercy upon which Lord Shiva gave him one of twelve Jyothirlingam with the condition that if it is placed in the ground it will get fixed to the ground. Ravana took the lingam was going towards Lanka. As he was on the way, he felt an urgent need to relieve himself.
Immediately Lord Vishnu appeared in form of a boy and offered to help Ravana to hold the lingam. Before Ravana could return, Lord Vishnu kept the Jyothirlingam on the ground and it got stuck in the ground.
In anger Ravana severed off the nine heads of Lord Vishnu. Lord Shiva with the help of a Vaidya revived Lord Vishnu by joining his heads to his body. Thus the temple got its name as Baidyanath.