Once he arrives in the castle, Beast gets tricked by a Heartless and gets sealed off in another part of the castle, leaving Sora alone. Sora fights Riku after getting back the Keyblade with the help of Donald and Goofy. Riku runs off wondering why he lost the Keyblade. The same hooded man that had spoken to Sora on the island at the beginning appeared behind Riku. He told Riku that he lost the Keyblade because his heart was weaker than Sora’s for that instant. He convinces Riku to give in to the darkness so that his heart can become stronger than Sora’s. After Sora battles against Maleficent, Riku appears to show off his own Keyblade and demonstrates how it can unlock hearts by unlocking Maleficent’s heart to the darkness.
After fighting against Maleficent in her dragon form, Sora arrives in the room where the Keyhole is. Riku/Ansem reveals to Sora that Kairi is one of the seven princesses and that her heart lies inside Sora. Sora and Riku/Ansem fight against one another and once Sora wins, he realises he needs to free Kairi’s heart to be able to close the Keyhole since it wasn’t fully formed. He uses Riku/Ansem’s Keyblade to unlock his heart. Six balls of light come out and go into the six princesses, then another one appears and floats inside Kairi. She wakes up just in time to try to catch Sora in his fall but he disappears. Ansem appears and tells Kairi she has served her purpose now that the Keyhole is open. He goes to walk towards her, but we see Riku’s spirit fighting off Ansem and telling Kairi to run away. After running off, Kairi, Donald and Goofy find out Sora has become a Heartless, but while Kairi was protecting him from other Heartless, he regained his human form.
The gang travel to Traverse Town to inform Leon of what happened and plan what to do next. He explains how Riku’s Keyblade was made of the princesses’ hearts and when Sora freed her heart the Keyblade was destroyed and the princesses’ hearts were also freed.
Sora goes back to Hollow Bastion without Kairi and finds out the princesses have stayed behind to try to hold back the darkness that has been coming out of the Keyhole. Sora makes his way back to the room where the Keyhole is and enters the door. After fighting against a monster Sora goes to seal the Keyhole when he hears Leon from the other side of the door. Leon tells him that once the Keyhole will be sealed, everyone will return to their respective world and there won’t be any way of traveling between them again. After saying goodbye, Sora enters the door and seals the Keyhole. The princesses still feel a great darkness and they tell Sora that it must be where Ansem is and so he leaves for the End of the World.
After some time, Sora finds a door and when he crosses it, he finds himself back on Destiny Islands. There, he finds Ansem and they fight against each other. After the fight, Ansem opens the door and tells Sora that not even his Keyblade can close the door to darkness. Sora disagrees with Ansem and tells him that he knows without a doubt that Kingdom Hearts is light. Once he’s said that, the door opens a bit and light comes out to engulf Ansem. After he disappears, Sora, Donald and Goofy run to the door to close it before the Heartless can come out. Riku shows up behind the door and tells him they can close it together. While he pulls the door from the inside and Sora, Donald and Goofy push from the outside, Mickey appears inside and fights off the Heartless while they try to close the door. Sora is reluctant to close the door while Riku is inside, but Mickey reassures him that there will always be a door to the light. They manage to close the door and with the help of Mickey’s Keyblade, they seal it shut.
Kairi appears behind Sora and he runs to her while the ground is separating. He grabs Kairi’s hand and promises her that he will find his way back to her.