Following the destruction of Ansem and Xemnas at the hands of Sora and Riku, Xehanort is revived. In response, Yen Sid puts Sora and Riku through a Mark of Mastery exam in order for them to become full-fledged Keyblade Masters and counter the coming return of Master Xehanort. As part of this trial, the two are separated from each other and sent to worlds previously destroyed by the Heartless that have since been restored, though now in a state of "deep sleep" which disconnects them from the other worlds.
These seven worlds are plagued by Dream Eaters — creatures born from darkness that seek out the sleeping worlds' Keyholes. Sora and Riku's given task is to reawaken the sleeping worlds by unlocking the seven Keyholes they find there and then return to the realm of light, recruiting benevolent "Spirit" Dream Eaters to guide them and battle the malevolent "Nightmare" Dream Eaters, after which they will be deemed Keyblade Masters.