Barbarika, the son of Ghatotkacha,
the grandson of Bhima,
was a fearless warrior.

He acquired the art of warfare from his mother
and was a staunch worshipper of Shiva and Shakti.

Shiva

Pleased with his devotion and behaviour, the goddess Kamakhya gave him three arrows. One that could identify and seek his enemies or those he would like to destroy, another to identify and mark his friends or those he would like to protect and the final third to do the deed to destroy the enemies and defend the friends.

When Barbarika learnt that the battle between the Pandavas and the Kauravas had become inevitable, he wanted to witness what was to be the Mahabharata War. Before leaving, he promised his mother, "if I feel the urge to participate in the battle, I will join the side which would be losing." Later, he started his journey to the field on his Blue Horse, equipped with his three indomitable arrows and a staunch bow.

DronacharyaArjuna

Krishna disguised, as a Brahmin, stopped Barbarika to examine his strength. The brahmin befriends Barbareek like a fellow traveller going to the battlefield and says, "the Pandavas and Kauravas are having a discussion on how long the war will last, how many days will you take to finish the war alone?". Barbarika answered, "I can finish it in a nimisha, a blink of an eye.

Alarmed at this but not showing it outwardly, Krishna asks Barbareek how he will instantly identify all Kauravas and Pandavas. Barbareek boasts that his arrows can find them all; all he has to do is think of them. This astonishes Krishna, and he asks him to prove it. He tells him to find all the leaves of the peepal tree they were sitting under and tie them around him (the brahmin) with his first 'seeker' arrow.

While Barbarik is preparing his first arrow, Krishna stealthily hides one leaf under his toe. The reason is to check if the arrows can also find things hidden by Krishna himself, who is omnipotent and an incarnation of the Supreme God Vishnu. To his astonishment, the arrow leaves Barbareek's bow, indeed ties all the leaves around Krishna, and then keeps hovering around his head.

The brahmin asks what this meant. Barbarik is also a bit surprised and says, "the arrow is probably targeting your foot to mark the leaf that is underneath". Krishna lifts his toe a little, at which the last leaf is visible. The arrow then immediately slashes through Krishna's toe, and the first arrow marks the hidden leaf. The third arrow then collects all the leaves (including the hidden leaf) and ties them together.

By this, Krishna concludes that the arrows are so powerful and infallible that even if Barbarika is unaware of the whereabouts of his targets, his arrows can still navigate and trace his intended targets. Thus Krishna gets a deeper insight into Barbarika's phenomenal power by asking

Krishna asks him if he had seriously thought about the consequences of giving such a word to his mother. Barbarika assumes that his support to the relatively weaker side will make them victorious. But, Krishna reveals the actual consequences of this.
Krishna tells him that whichever side he supports will end up making the other side vulnerable due to his power. Hence, he will be forced to switch sides to support the other side that has become weaker. Thus, in an actual war, he will keep oscillating between two sides, thereby destroying the entire army of both sides and eventually, he would be the lone survivor.

Hence, Krishna avoids his participation in the war by seeking his head in charity.
In a quandary, Krishna finally decides to reveal his true form to Barbareek. As Barbareek sees Krishna in his Vishvaroop Vishnu form, he is overcome with emotions. The act of violating Lord Vishnu himself was beyond him.
Krishna then explained to him that before a battle, the head of the bravest Kshatriya needs to be sacrificed to worship/sanctify the battlefield and that he considered him to be the most valiant among Kshatriyas and was asking for his head in charity. In fulfilment of his promise and in compliance with Krishna's command, Barbarika gave his head to him in charity.

Pleased with Barbarik, Krishna asks him to demand two boons. After a few moments of thought, Barbareek says, "I want to see the entire battle till the end". Krishna agreed and placed the head on top of a hill overlooking the battlefield. At the end of the battle, the victorious Pandava brothers argued among themselves about who was responsible for their victory. Krishna suggested that Barbarika's head, which had watched the whole battle, should be allowed to judge.

On asking about it, Barbarika replies, "All I could see were two things. One, a divine chakra spinning all around the battlefield, killing all those who were not on the side of Dharma. The other was the Draupadi who has taken her original form of Goddess Mahakali, who spread out her tongue on the battle field and consumed all the sinners as her sacrifice".

Listening to this, Pandavas realized that it was Lord Narayana and Goddess Mahakali who actually eradicated the Adharma from the world, and the Pandavas were mere instruments.