There are many interesting legends related to Kaal Bhairav, one of the eight avatars of Lord Shiva. The origin of the mighty god is attributed to a tale in the Shiv Mahapuran.
Once sage Agastya asked Skanda ji that who is this bhairav in Kashi. How they appear and how to worship them? Skanda said, in ancient times, several great Sages sitting on the top of Sumeru Mountain desired to know from Lord Brahma as to who was indestructible and supreme. Lord Brahma proclaimed himself to be that superior person. On hearing this, Yagyeshwar (symbolic of Lord Narayan) reprimanded Lord Brahma for his hasty and audacious utterances and said “Why are you saying these things without knowing about it? It is not appropriate for a yogi like you to have such irrelevant talks.
I am the supreme among all.With the mutual intention of victory, both of them asked the same question to the four Vedas.
Rig Veda replied, “Within which all the ghosts are located, from which all of these things are promulgated, which is called by the word “tat” by various priests, He is one and only Paramtatva Rudra.
The Yajurveda replied, “Those who are being meditated by all Yagnas and Yogis, with whose force we are considered to be the evidence, the same paramtatva Shiva is supreme.”
Sam Veda stated that the respected figure who controls the entire world, who is contemplating by various yogis, with whose light the world shines, that paramtatva is none other than Triambak.”
Finally, Atharva Veda said, all human beings can see the Lord through Bhakti Marg and such a deity who can remove all the worries of human beings, that paramtatva is indeed Shankar. In a nut shell, all the four Vedas proclaimed that Lord Shiva is the ultimate being.
Both Lord Brahma and Yagya Narayan started laughing in disbelief. Shortly Lord Shiva appeared as a powerful divine light in their midst. Lord Brahma stared at that being furiously with his fifth head. Lord Shiva immediately created a new being and stated that he (the new being) will be the King of Kaal and will be known as Kaal Bhairav. (Here Kaal is referred to final time viz. death). Lord Shiva further stated that Kaal Bhairav will remove all the sins of his devotees immediately and therefore, he will be known as Paap Bhakshak.
Lord Shiva also granted the lordship of Kashi to him. Lord Shiva ordered Kaal Bhairav to punish the Pankajjanma (Brahma).
Meanwhile, Lord Brahma’s fifth head was still burning with fury and Kaal Bhairav plucked that head with the little finger of his left hand (just as we pluck flowers from the garden). The people present there, along with Yagyamurtidhaari Vishnu, immediately started singing praises of Lord Shiva. Lord Shiva said to Bhairav that both Yagyaprush and lord Brahma are honorable to you, You will hold the head of lord brahma, ask for alms always and have customary fasting to remove the sin of brahmahatya and set an example for the world.
You can go anywhere but the sin of Brahmahatya (affliction on account of hurting a Brahmin) will never leave you behind. Lord Shiva created a dangerous and fierce looking female figure, named Brahmahatya and asked her to follow Kaal Bhairav wherever he went. But, you will not be able to enter Moksh City Varanasi.
By the proximity of Brahmahatya Bhairav become dark. As Directed by Lord Shiva, Kaal Bhairav, with Brahma’s head in his hand and having customary fasting, started going around various places in the world, took bath in various Teerths, worshipped various Lords, nevertheless saw that Brahma Hatya was following him all along. He could not get rid of that affliction, that sin.
Finally, Kaal Bhairav reached the Moksha Puri, Kashi. The moment Kaal Bhairav entered Kashi, Brahma Hatya started shouting, screaming and finally disappeared into the netherworld. The head (Kapal) of Brahma, fell at a place which was called Kapal Mochan and there was a Teerth which was later called Kapal Mochan Teerth.
Then onwards Kaal Bhairav stationed himself permanently in Kashi, giving shelter to all his devotees. Those living in or visiting Kashi, must worship Kaal Bhairav and he grants protection to all his devotees.
Once, the gods Brahma and Vishnu were competing for supremacy. Then Shiva appeared as a powerful divine light between the duo. An angry Brahma stared at the pillar of light with his 5th head, which started to burn out of anger. Shiva has instantly created the new being Bhairava or Kaal Bhairav (King of "Kaal": "Kaal Bhairav"), who decapitated Brahma's 5th head. However, the head stuck to Kaal Bhairav's hand.
Shiva then ordered Kaal Bhairav to go to various places but Brāhmanahatya, the sin of beheading Brahma personified as a gory woman, followed him until he reached Varanasi where Brahma's head fell on the ground. The place is called "Kapal Mochan Teerth". He was freed of the sin of Brāhmanahatya as he entered Varanasi. Shiva then pronounced that "Kaal Bhairav" will forever be in Varanasi in order to remove sins of his disciples.