When Madara came back to life and stole Obito's half of the power, he turned into the Sage of Six Paths. He used the power of the forbidden art to trap everyone in the Ninja World (except Naruto, Sasuke, Sakura, Kakashi, Obito and other dead ninjas). Black Zetsu fused with Madara to complete the reincarnation. That freed Kaguya Otsutsuki to take his place. In the past, Kaguya's son, Hagoromo, had trained Ashura and Indra. Ashura and Indra were the first incarnations who came before Naruto and Sasuke. After Kaguya killed Obito, Kakashi sacrificed his power. Naruto and Sasuke worked together to seal Kaguya and Zetsu, and Madara died again. When Sasuke used the forbidden genjutsu, Naruto defeated and spared him. The world was peaceful and all the spiritual beasts were freed from Kaguya. Kakashi became Sixth Hokage. Sasuke realized his mistake, disabled the genjutsu, and forgave everyone.