Once, while the gods Vishnu and Brahma competed for the superiority of who is the creator, Lord Shiva appeared as a flame and challenged them to find their source.

Brahma took the form of a swan, and flew towards the sky to see the peak of the light. While Vishnu became Varaha, and sought its bottom basis.
This scene is called Lingodbhava and is depicted in the western wall in the sanctum sanctorum of most Shiva Jyotirlinga.

Neither Brahma nor Vishnu could find the source And when Vishnu accepted his defeat, Brahma lied and said that he had got the pinnacle.

Enraged at this, Lord Shiva took the form of Bhairava and separated one of the five ends of Brahma.

In the punishment, Shiva ordered that Brahma would never keep temples on earth for his worship.

As soon as Shiva cut off Brahma's fifth head, he had sinned "Brahmahatyap" (hurting a Brahmin) and had to roam around in three realms as a naked, beggar to be freed from his sin.
The sin finally becomes free in Varanasi on the banks of the river Ganga The legend is detailed in the Vishnu Purana, the pillar of fire commonly known as jyotirlinga.