The film begins with the magic deal-maker Rumpelstiltskin reading from a book called "Shrek Forever After", which tells of what happened just before Princess Fiona’s rescue by Shrek during the first film. King Harold and Queen Lillian, desperate to break Fiona's curse, meet with Rumpelstiltskin. He tells King Harold he will end Fiona's curse in exchange for the throne. Harold is about to sign the contract, but one of the king's servants bursts in and word comes that Fiona has been rescued, causing King Harold to rip the contract in half. In the present, a disgruntled Rumpelstiltskin tears pages of the "Shrek Forever After" book out and is kicked out of the library. He then wishes Shrek was never born, vowing vengeance.
Shrek has grown tired of being a celebrity and his overwhelming family and friends, as well as feeling that he is not a “real ogre” anymore. At the ogre babies' first birthday, fans ask Shrek to autograph pitch forks and torches, a boy asks him to roar, and the three pigs eat the birthday cake. Coupled with the growing frustration of his repetitive home life, Shrek roars in frustration and everyone applauds. Moments later, he then smashes a new cake and angrily leaves. Fiona talks to Shrek about his anger and his wish to be a real ogre, overheard by Rumpelstiltskin. Fiona tells Shrek that he has everything, but does not see it, and returns to the party.
Walking home, Shrek encounters Rumpelstiltskin, who offers him a day to feel like a real ogre again, in exchange for a day from Shrek’s childhood. Suspicious but happy at the prospect of a relaxing vacation, Shrek signs the contract, and abruptly lands in a world where he is still feared by villagers. Shrek enjoys causing ogreish mischief before he sees wanted signs for him and Fiona and discovers that his home has become a deserted tree stump. Shrek is attacked by a group of witches and knocked unconscious. Shrek wakes up in a cage being hauled by Donkey, who does not recognize Shrek. Shrek is brought to Rumpelstiltskin, king of Far Far Away. Rumpelstiltskin reveals that the day Shrek traded away was the day he was born. Since he was never born, Fiona was never rescued, and Queen Lillian and King Harold signed the contract to make "all of their problems disappear", causing them to literally disappear; thus, Rumpelstiltskin took over, and has enslaved the ogre population. Rumpelstiltskin then reveals that when his day is up, Shrek will be erased from existence as well. Shrek steals a broom and escapes with Donkey. Though Donkey is initially afraid of Shrek, he returns when he finds Shrek crying over his babies' toy. Having never seen an ogre cry before, Donkey decides to trust him.
Donkey reveals to Shrek the escape clause of Rumpelstiltskin’s contract: if Shrek receives "True Love’s First Kiss", the contract will be rendered null and void. After not finding Fiona in the tower, the two discover a secret community of ogres led by Fiona. Fiona, who does not recognize Shrek, dismisses his attempts to reconcile with her. Shrek also finds Puss in Boots, now Fiona’s pet who retired from swordsmanship and is fat and lazy. Puss, at first doesn't recognize Shrek, but befriends him when he realizes Shrek's love for Fiona.
Rumpelstiltskin is to lead the nightly ogre hunt, and Fiona plans to ambush him. However, Rumpelstiltskin has hired the Pied Piper, whose magical flute entrances the army of ogres to dance themselves to the castle (to the song "Shake Your Groove Thing"). Puss and Donkey save Fiona and Shrek. Shrek and Fiona kiss, but since Fiona does not love him their kiss is ineffective. Fiona leaves to save the other ogres, without Shrek, Donkey and Puss.
Rumpelstiltskin announces that whoever brings him Shrek will get whatever they want with no strings attached. Shrek turns himself in in return for "all ogres going free". Shrek is imprisoned with Fiona; since Fiona is a human by day, she was not released, but is moved by Shrek's sacrifice and she falls in love with him. The two are to be fed to Dragon, Donkey's wife in the standard timeline, but Puss, Donkey and the ogres arrive and battle Rumpelstiltskin’s forces, eventually winning and imprisoning Rumpelstiltskin.
As Shrek and Fiona escape and the day is almost over, Fiona kisses Shrek, but he disappears. Fiona remains an ogre as the sun rises, and realizes that was true love's kiss. The contract is destroyed, the alternate world falls apart, and Shrek is sent back in time to when he roared during his kids' birthday. Shrek embraces Fiona, his family and friends, and remarks that Fiona saved him when they met.
Before the credits, Shrek, his family and friends, including the ogre army, celebrate the ogre babies' first birthday. At the larger party, Rumpelstiltskin is held prisoner and tormented by the Piper, and his pet goose explodes due to Fiona's singing. A curtain falls on the credits with a montage of clips from Shrek, Shrek 2, and Shrek the Third.