The film opens with Phineas (Vincent Martella), Ferb (Thomas Sangster), Candace (Ashley Tisdale), Perry, a.k.a. Agent P (Dee Bradley Baker), and Heinz Doofenshmirtz (Dan Povenmire), who have been captured by the 2nd Dimension Doofenshmirtz and in chains, being led to a large, hairy, caged monster called a "Goozim". The scene flashes back to earlier in the day when Phineas and Ferb have just woken up. It's been five years since they adopted their pet platypus, Perry, from the O.W.C.A.'s animal shelter. After reminiscing about how they got him ("Everything's Better with Perry"), the boys decide to play giant platypus badminton, and construct a platy-pult with their friends Isabella (Alyson Stoner), Buford (Bobby Gaylor), and Baljeet (Maulik Pancholy). Perry the Platypus reports to his lair and dons his secret agent persona, Agent P. He is briefed by Major Monogram (Jeff "Swampy" Marsh) about his mission, receives new gadgets by Carl (Tyler Alexander Mann), and rides off in his rocket car. Phineas and Ferb launch themselves off the platy-pult in a giant shuttlecock and fly through the air, but end up colliding with Perry's rocket car and crash onto the roof of the Doofenshmirtz Evil Incorporated building, destroying Dr. Doofenshmirtz's latest invention, the Otherdimensionator.
Meanwhile, upon hearing that her boyfriend Jeremy Johnson (Mitchel Musso) is already looking for the perfect college for himself, Candace, Phineas and Ferb's older sister, decides to grow up and act more like an adult with the help of her best friend, Stacy Hirano (Kelly Hu). Phineas and Ferb apologize for breaking Doofenshmirtz's invention and offer to help him fix it. Perry, who has arrived on the scene, goes back into his mindless pet persona, lest his owners find out his secret. The boys and the evil scientist successfully open a portal to the 2nd Dimension, and find that it is ruled by an alternate Doofenshmirtz, who has seized control of the Tri-State Area, unlike his prime counterpart. Doofenshmirtz meets him ("A Brand New Best Friend (and It's Me)").
Phineas, Ferb, and Perry follow, and Doof-2 reveals Perry's identity as a secret agent by making his minion, Platyborg (who was the 2nd Dimension Perry defeated by Doof-2 and turned into a cyborg) attack the boys, to which Perry had no choice but to save them. The boys and their pet make a hasty escape from the building, with Phineas feeling angry and betrayed that his pet kept this secret from him this whole time. The three try to get back to their dimension by using a remote version of the Otherdimensionator, but are unable to. They decide to go find their 2nd Dimension selves for help, and find out that that summer has been outlawed in this Tri-State Area and that they have never heard of summer ("Summer (Where Do We Begin?)"). Perry sees a news report on the TV saying that if Perry turns himself in, Doof-2 would not hurt Phineas and Ferb. As Perry goes to surrender himself to Doof-2, Phineas, still angry at him for not being honest with them all these years, disowns him, forcing Perry to sadly leave ("I Walk Away").
Phineas, Ferb, and their 2nd Dimension selves then go across the street to get help from the 2nd Dimension Isabella, only to be stopped by a patrolling Normbot (one of the mass-produced 2nd Dimension versions of Doofenshmirtz's robotic servant Norm; both are voiced by John Viener), who is taken down by the 2nd Dimension Buford. The five get across the street, but they all fall down a chute into the headquarters of the Resistance, a group of civilians who want to overrule Doof-2's dictatorship. They are tied up by the Firestorm Girls and presented to Isabella-2, who recognizes at least Buford-2, and she lets the group go. Phineas explains their dilemma, and they are taken to "Dr. Baljeet", the 2nd Dimension version of Baljeet. Dr. Baljeet explains the concept of inter-dimensional time travel, and that they would need a lot of energy to get to the 1st Dimension. Candace-2 reveals herself to be the Resistance's leader and they open a portal to Phineas and Ferb's backyard. Upon hearing that Perry has been captured, Phineas and Ferb want to go back to save him. Candace and Stacy see the portal, and Candace steps through, causing the portal to close, leaving her stuck in the 2nd Dimension.
The gang decides to break into Doof-2's headquarters to rescue Perry by using underground mine carts, but find that it is a trap. Perry is able to distract the Doofs and the Norm Bots by showing a hologram of Major Monogram in the shower. The gang tries to escape by heading back with the mine carts, but Candace-2 separates the carts between the Resistance and the 1st Dimension-ers, so she and her brothers are safe, leading to Phineas, Ferb, Candace, and Perry's capture. Phineas and Ferb refuse to rebuild the Otherdimensionator so Doof-2 can go to the 1st Dimension and enslave it, so he sentences them to be doomed (i.e., fed to the Goozim). He also sentences the regular Doofenshmirtz, as he annoys him. As Phineas, Ferb, Candace, Agent P, and Doofenshmirtz are about to be eaten, Candace-2 shows up with the remote version of the Otherdimensionator, destroys the Norm Bots, and throws the remote to Phineas, who opens a portal and escapes along with his friends. They travel through numerous dimensions ("Brand New Reality"), until they get back home. They find that Doof-2 has arrived in Danville, the Otherdimensionator having been repaired by Doofenshmirtz prior to his sentencing to the Goozim, and that Normbots are invading the city. While Perry goes to fight with his fellow agents, Phineas and Ferb simply walk home.
Once there, they find Agent P's lair, where they get a message from the O.W.C.A. stating that they need their help. Many of the boys' inventions from past episodes are recreated, and the kids of Danville use them to combat the Norm Bots attacking the Tri-State Area ("Robot Riot"). Phineas and Perry get to the top of the Doofenshmirtz Evil Incorporated building, where Doof-2 is. After battling him and Platyborg, Phineas and Perry are able to destroy the satellite controlling the Norm Bots, and they all fall to the ground. As Candace decides that she doesn't want to be an adult anymore, she suddenly comes up with a plan to save the Tri-State Area, and she goes to get her parents (Caroline Rhea and Richard O'Brien) to see what's going on. Ferb joins Phineas and Perry on the roof as Doof-2 rises from the floor with a giant robotic version of himself. As he is about to smash and kill the trio, the regular Doofenshmirtz shows up and hands Doof-2 a toy train. Doof-2 had lost his toy train when he was a kid, and that had caused him to become evil. Reunited with his object, Doof-2 apologizes and reveals that he has had self-destruct buttons on all the Norm Bots after all, and uses a remote to destroy all of them. Candace and her mother come out and find that everything is back to normal and that Candace has once again failed to bust her brothers. However, she takes it as, since the "mysterious force" takes thing away before her mother sees it, by bringing her mother out, and causing the Normbots to be taken away, she has saved the world.
Doof-2 returns to the 2nd Dimension, only to be arrested by Major Monogram and the Firestorm Girls. Monogram and Carl tell Phineas and Ferb that Perry will have to go away forever now that they know his secret revealing the reason why Perry never told them his secret, but they then remember that Doofenshmirtz had built an Amnesia-inator to erase memories. Doofenshmirtz has no memory of building such device (proving that it worked, as it had erased his memory of constructing it). Phineas, Ferb, and their friends line up at the O.W.C.A. headquarters to get their memories erased and forget the entire day. Phineas and Ferb say their last good-bye to Agent P, and Isabella kisses Phineas on the lips, finally being able to live out her fantasy. Before Phineas can say anything, the Amnesia-inator does its work. Afterward, Agent P enters his lair, and takes out a camera. He plugs it into the screen and looks at photos of his and his owners' adventure, smiling ("Takin' Care of Things").
The film ends with Slash performing "Kick It Up a Notch" with Phineas, Ferb, their friends, and Doof-2.