Long before the founding of the hidden villages, during an era of endless wars between mankind, the God Tree, a tree revered as a deity, bore a chakra fruit once every thousand years. Although it was forbidden to consume the God Tree's fruit, it was eaten by Princess Kaguya Ōtsutsuki, who used the power it granted her to end the wars plaguing the lands. Some time after this, Kaguya gave birth to two sons who became the first people born with chakra. Later, deciding that her children's chakra was rightfully hers, Kaguya combined with the God Tree to become an entity known as the Ten-Tails, which ravaged the land in an attempt to obtain said chakra. However, because Kaguya's true motives were not realised, it was instead believed that the God Tree itself had taken on the form of a monster in an attempt to regain the chakra that Kaguya had stolen.
In the final battle between the Ten-Tails and Kaguya's sons, Hagoromo and Hamura, they managed to defeat the beast by sealing its body into the moon, and sealed its chakra within Hagoromo, making him the first jinchūriki. This act, along with many other great deeds throughout his lifetime, resulted in Hagoromo becoming revered as the "Sage of Six Paths". However, knowing that his death would unleash the Ten-Tails back into the world to continue its rampage and reclaim the chakra he had spread among humanity, Hagoromo separated the monster's chakra from its body and used his Creation of All Things Technique to create the nine tailed beasts. Hagoromo then used Chibaku Tenseito seal the Ten-Tails' husk in what would become the moon.
Unbeknownst to the brothers, Kaguya had created Black Zetsu prior to her sealing in order to facilitate her revival. To this end, it spent centuries attempting to unite the chakra of Hagoromo's sons, Indra and Asura, in order to cause one of their reincarnations to awaken the Rinnegan. Eventually, thanks to Black Zetsu's machinations, the Rinnegan would once again manifest itself in one of Hagoromo's descendants: Madara Uchiha. With his new power, Madara summoned the empty vessel of the Ten-Tails back into the world and named it the Demonic Statue of the Outer Path. Joined by Obito Uchiha, who continued his work after his death, Madara devised a means to reunite the Demonic Statue and the tailed beasts back into their original form so he could use the Ten-Tails to enact his lifelong ambition: the Eye of the Moon Plan.