Hindu scriptures and mythology is the source of all the Hindu rituals and festivals. In fact, one can find explanations for all the Hindu traditions and customs in the religious epics. Even the origin of Rakhi celebrations is rooted in the Hindu mythology. There is no confirmed date and time available about the history of Rakhi festival, but the stories are many. Started as the 'different tradition' of tying the thread to the husband, it has evolved to having the sisters tying the thread to their brother. The different stories related to this festival, which explain the history behind it, go as follows...
In the Vedic period, on a 'Shravan Poornima' day (Full Moon Day of the Hindu month of Shravan), the deities and demons were fighting a battle against each other. Unfortunately the demons were in a stronger position, as compared to the deities. The king of the deities, Lord Indra, was very much worried about the result of the battle. His wife Indrani (also known as Shashikala) could not see him worried and prayed to the almighty to help her husband. Indrani was a religious lady, so she prepared a talisman with her religious power and tied it around Indra's right wrist.
Indrani believed that her talisman will safeguard Indra from the attack of demons. Eventually, she proved right, as that day, the deities won the battle and Lord Indra escaped unhurt. As the talisman had the power of protecting the person who wore it, it came to be known as 'Raksha Sutra' and the ceremony of tying it was called 'Raksha Bandhan'. Since this particular act of tying the talisman took place on 'Shravan Poornima' day, it has become a tradition to celebrate 'Raksha Bandhan' on the 'Shravan Poornima' day every year. With time, the festival came to comprise of brother-sister duo, rather than husband-wife.
It is said that when Alexander invaded India in 326 B.C., his wife tied a Rakhi to the king Porus and in return, Porus promised to protect her and her husband. We also have various such examples of tying knots or threads, especially in the history of Rajputana. The most famous of them is the story of the Queen Karnavati of Chittor, who sent a Rakhi to the Mughal emperor Humayun, to save her kingdom from the invasion of Bahadur Shah of Gujarat. That particular day of Purnima was celebrated as Raksha Bandhan first in Marwar and then, all over Rajasthan. Finally, it came to celebrated all over India.