It refers to a battle between gods and demons. Indra (the king of the gods) was feeling dejected, so his wife Sachi took a thread, charged it with sacred verses (Mantras) for protection and tied it on Indra's hand. The strength of this thread helped Indra to conquer his enemies. This festival is celebrated since then.
In the Epics
In Mahabharata, Lord Krishna advised Yudhishtthir to tie the puissant Rakhi to guard himself against impending evils. Thus the Raksha Bhandhan symbolizes the protection of the good from evil forces. In the ancient Puranik scriptures, it is said that King Bali's source of strength had been the Raakhi. Hence while tying the rakhi this couplet is usually recited:
"Yena baddho Balee raajaa daanavendro mahaabalah tena twaam anubadhnaami rakshe maa chala maa chala"
"I am tying a Rakhi on you, like the one on mighty demon king Bali. Be firm, O Rakhi, do not falter." Rani Karnawati and Emperor Humayun
During the medieval era, Rajputs were fighting Muslim invasions. Rakhi at that time meant a spiritual binding between siblings and the protection of sisters was foremost. A famous incident relates how rakhi broke religious barriers. When Rani Karnawati, the widowed queen of the king of Chittor realized that she could in no way defend the invasion of the Sultan of Gujarat, Bahadur Shah, she sent a rakhi to Emperor Humayun. The Emperor touched by the gesture set off with his troops in aid of Chittor without wasting any time.
Alexander the Great and King Puru
The oldest reference to the festival of rakhi goes back to 300 B.C. at the time when Alexander invaded India. It is said that the great conqueror, King Alexander of Macedonia was shaken by the fury of the Indian king Puru in his first attempt. Upset by this, Alexander's wife, who had heard of the Rakhi festival, approached King Puru. King Puru accepted her as his sister and when the opportunity came during the war, he refrained from harming Alexander.