Barbarika was originally a yaksha, and was reborn as a man.
He wanted to fight on the Pandava side, but he was forced to stick to his principle of always fighting on behalf of the losing side.
He learnt the art of warfare from his mother. Shiva, pleased with him, gave him the three infallible arrows. Hence, Barbarika came to be known by the appellation Teen Baandhaari, the 'Bearer of Three Arrows.' Later, Agni (the God of Fire) gave him the Bow that would make him victorious in the three worlds.
The omnipresent Krishna, disguised as a Brahmin, stopped Barbarika to examine his strength. He baited Barbarika by mocking him for going to the great battle with only three arrows. On this, Barbarika replied that a single arrow was enough to destroy all his opponents in the war, and it would then return to his quiver. If all the three were used, it would create havoc in the three worlds. Krishna challenged him to tie all the leaves of the peepal tree under which he was standing, with one arrow. Barbarika accepted the challenge, removed one arrow from his quiver and released it from his bow. The arrow tied all the leaves together within moments. However, Krishna had held one leaf under his foot, and the arrow started revolving around it. Krishna then asked the boy whom he would favour in the war. Barbarika revealed that he intended to fight for whichever side appeared set to lose. Krishna knew that the defeat of the Kauravas was inevitable. He judged that if this brave boy joins their side, the result would then tilt in their favour.
The Brahmin (Krishna) then sought charity from the warrior. Barbarika promised him anything he wished. Krishna asked him to give his head in charity. Barbarika was shocked. Perceiving that all was not as it appeared, he requested the Brahmin to disclose his real identity. Krishna showed Barbarika a vision of His Divine Form and Barbarika was thus graced. Krishna then explained to him that before a battle, the head of the bravest Kshatriya needs to be sacrificed, in order to worship/sanctify the battlefield. Krishna said that he considered Barbarika to be the bravest among Kshatriyas, and was hence asking for his head in charity. In fulfilment of his promise, and in compliance with the Lord's command, Barbarika gave his head to Krishna in charity. This happened on the 12th day of the Shukla Paksha (bright half) of the month of Phalguna.
Krishna, pleased with Barbarika's great sacrifice, granted him the boon that when Kaliyuga descends, he would be worshipped by the name of Shyam in his form. His devotees would be blessed just by pronouncing his name from the bottom of their hearts.
Before decapitating himself, Barbarika told Krishna of his great desire to view the forthcoming battle, and requested him to facilitate it. Krishna agreed, and placed the head atop a hill overlooking the battlefield. From the hill, the head of Barbarika watched the whole battle.
At the end of the battle, the victorious Pandava brothers argued amongst themselves as to who was responsible for the victory. Krishna suggested that Barbarika's head, which had watched the whole battle, should be allowed to judge. Barbarika's head suggested that it was Krishna alone who was responsible for the victory: his advice, his presence, his gameplan had been crucial. Barbarika's head said that he had seen the Sudarshana Chakra revolving around the battlefield, hacking the Kaurava army to pieces; and Draupadi, assuming the fearful form of Mahakali Durga, drinking bowl after bowl of blood without allowing even one drop of blood to fall on the earth.
When Barbarika learnt that battle between the Pandavas and the Kauravas had become inevitable, he wanted to witness what was to be the Mahābhārata War. He promised his mother that if he felt the urge to participate in the battle, he would join the side which would be losing. He rode to the field on his Blue Horse (Neela Ghoda) equipped with his three arrows and bow.