A hundred years ago, Godey, the architect who designed an enormous set of musical instruments in Alamos Town called the Space-Time Towers, had a nightmare which he wrote down in his diary, which is later discovered by his great-grandson, the scientist Tonio. The nightmare foretold that two entities that should never have met would cross paths in the space-time rift. Their wrath from this meeting will envelop the town, and cause its collapse. In a garden designed by Godey, Darkrai, a nightmare-causing Pokémon hated by everyone else, opened its heart to a girl called Alicia, who invited Darkrai to stay in the garden. Godey realized the nightmare was telling him to make a music disc for “Orácion”, a song with the power to soothe the fiercest rage, for future use. Years later, Darkrai saved the life of Alicia’s granddaughter Alice after she tripped and fell from a cliff.
In the present, Godey’s foretelling comes true when the Pokémon Palkia, who can control space, and Dialga, who rules over time, cause disturbances in the air around Alamos Town as they brawl in the space between dimensions. At the same time, Ash Ketchum, Brock, and Dawn arrive in the town for Dawn’s next Pokémon Contest inside the Space-Time Towers. Palkia and Dialga’s clashing causes a disturbance in space which damages the garden. Baron Alberto believes this is the work of Darkrai. While battling Alberto’s Lickilicky, Darkrai makes Ash fall asleep and creates a nightmare where Palkia attacks Ash. Darkrai then appears before disappearing down a hole that Ash and his Pikachu are sucked into.
The next day, Palkia, injured and attempting to hide from Dialga, emerges in Alamos Town. It moves the town to another dimension with no way of escaping. Darkrai reappears in the town square telling someone to go away. Alberto, joined by Ash, Dawn, and other Pokémon trainers, engage in combat with it. Outraged, Darkrai traps many Pokémon in the town square in nightmares. Later that evening, Tonio finds Palkia resting between the Towers. Darkrai tries to attack the resting Palkia. Ash then realizes that Palkia was the one that Darkrai was telling to go away and that Darkrai’s nightmare was warning Ash about Palkia.
As Palkia is about to hit Darkrai with one of its attacks, Dialga arrives, opens fire on the two and immediately runs into Palkia. As Palkia and Dialga brawl, the entire town slowly starts to collapse. After Alice finds the “Oración” music disc, Ash and Dawn climb up to the music disc player on the Space-Time Towers’ skybridge while Brock helps evacuate the townspeople. Tonio reveals that if Palkia and Dialga collide once more, the dimension they are in will be destroyed. When Palkia and Dialga discharge their attacks, Darkrai crosses the attacks’ line and forms a sphere around itself to block the attacks, engulfing and immobilizing Palkia and Dialga in the process.
After breaking free, Palkia and Dialga both attack Darkrai, causing it to disintegrate. Ash and Dawn realize that the Space-Time Towers are no-longer charged, so Pikachu along with Dawn’s Pachirisu use their electrical powers to charge the Towers as they play the “Orácion” disc. The song successfully calms Palkia and Dialga down. Dialga flies away, and Palkia’s wound heals. Ash and Dawn successfully plead to Palkia to restore the lost parts of the town, returning Alamos Town to how it originally was, and the townspeople and Pokémon rejoice and return home. Later, the group mourns the loss of Darkrai, with Alice thanking it for its efforts. They then find Darkrai standing on top of the Space Tower.
In the end credit montage, Dawn fails to win a ribbon at her Pokémon contest.