Just before Princess Fiona’s (Cameron Diaz) rescue by Shrek (Mike Myers) in the first film, King Harold (John Cleese) and Queen Lillian (Julie Andrews) are revealed to have come to the magic deal-maker Rumpelstiltskin (Walt Dohrn) for help with breaking Fiona’s curse. King Harold is about to sign a contract to break the curse in exchange for Rumpelstiltskin becoming king of Far Far Away, but word comes that Fiona has been rescued, causing King Harold to decline the offer. A disgruntled Rumpelstiltskin in the present day wishes that Shrek was never born, vowing vengeance.
Meanwhile, Shrek has grown tired of his life being a local celebrity and his overwhelming family and friends, as well as the fact he feels he isn’t a “real ogre” anymore. When they arrive at Far, Far Away for the ogre babies' first birthday, a mob of fans ask Shrek to autograph pitch forks and torches, a boy asks him to do his roar, and the three pigs eat the birthday cake. All this makes Shrek roar in frustration and everyone applauds. He then smashes a new cake and angrily leaves the room. Outside, Fiona talks to Shrek about his anger and his wish to be a real ogre again. Rumpelstiltskin overhears this. Fiona tells Shrek he has everything, but doesn't see it, and she heads back inside the castle. Shrek leaves and goes on a walk ranting about how Fiona isn't in charge and how she doesn't deserve an apology. In the middle of his rant he sees Rumpelstiltskin faking to be hurt, After "rescuing" him Rumpelstiltskin insists he joins him for a ride in his carriage.
Rumpelstiltskin offers him a day where none of his adventures had ever happened, a day to feel like a real ogre again, in exchange for a day from Shrek’s childhood. Shrek agrees and signs the contract. Shrek enjoys his day being feared by villagers, until he sees his home in the swamp is nothing but a big tree stump. He is captured by a group of witches and taken to Rumpelstiltskin, the king of Far, Far Away; Rumpelstiltskin took the day Shrek was born and even successfully makes King Harold and Queen Lillian sign the contract, causing them to disappear, as the contract had stated that it would make "All of their problems disappear", thereby making him king. He even enslaves the ogre population. Rumpelstiltskin declares that once the 24 hours is up, Shrek, having never been born, will cease to exist altogether.
Shrek is able to escape his confinement and rescues Donkey (Eddie Murphy), who works as a pack mule and has never met Shrek. Donkey at first doesn’t trust Shrek and fears him, but eventually becomes friends with the ogre. Donkey is also intelligent and is able to deduce by folding the contract paper that the exit clause of Rumpelstiltskin’s contract is true love’s first kiss before the 24 hours run out. Not finding Fiona in the tower, the two stumble upon a secret community of warrior ogres led by Fiona (all the ogres are taller than Shrek). Fiona has never met Shrek, can't stand him and dismisses his attempts to reconcile with her. Shrek also finds Puss in Boots (Antonio Banderas), now Fiona’s pet who retired from swordsmanship and is now obese and lazy. Puss, although he doesn't know Shrek, befriends him and realizes that there was some romantic interest between Shrek and Fiona.
Rumpelstiltskin is to lead the nightly ogre hunt throughout the kingdom, and Fiona plans to defeat him along the route. However, Rumpelstiltskin has hired the Pied Piper, who with a magical flute entrances the army of ogres to dance themselves all the way to the castle (to the song Shake Your Groove Thing.) Puss and Donkey are able to save Fiona and Shrek. Shrek has by this point all but won Fiona’s heart, but since Fiona doesn’t truly love him, their kiss doesn’t fix the spell. Fiona leaves to save the ogres and the kingdom without Shrek, Puss, or Donkey.
Rumpelstiltskin announces to the kingdom that whoever brings him Shrek will get whatever they want with no strings attached. Shrek turns himself in and in return Rumpelstiltskin frees the ogre army. Shrek is imprisoned along with Fiona, since Fiona is a princess by day, she could not go free, but is deeply moved by Shrek's self-sacrifice. The two are to be fed to Dragon, Donkey’s wife in the regular timeline, but Puss, Donkey, and the ogres arrive and battle Rumpelstiltskin’s forces.
As Shrek and Fiona escape and the 24 hours are almost going to pass, Fiona kisses Shrek and he disappears. Fiona remains as an ogre as the sun rises, and she realizes that was true love's kiss. The witches, the ogres, Donkey, Puss and Fiona disappear and the contract is destroyed, sending Rumpelstiltskin to his defeat and the town is back to normal when Shrek is roaring in frustration in his kids' birthday. Shrek embraces Fiona, his family, and friends again, and remarks that Fiona had in fact saved him when the two met. After which, we see Shrek's hand close up the book and put it with the books of the other three films, the next book titled "Puss In Boots: The Story of an Ogre Killer".
During the end credits, they and all their family and friends including the ogre army, celebrate the ogre babies’ first birthday in a larger party which includes Rumpelstiltskin, now held prisoner and his pet goose exploded due to Fiona's singing. A curtain call follows in the credits, along with a montage of clips from the past three films: Shrek, Shrek 2, and Shrek the Third.