When Shrek (voiced by Mike Myers) and Fiona (voiced by Cameron Diaz) come back from their honeymoon, they find an invitation to a royal ball with Fiona's parents to celebrate their marriage, an event Shrek is reluctant to participate in. Fiona talks him into it, and along with Donkey (voiced by Eddie Murphy), they travel to the kingdom of Far Far Away. They meet Fiona's parents, King Harold and Queen Lillian (voiced by John Cleese and Julie Andrews), are repulsed by Shrek being an ogre. At a shared meal, Shrek and Harold get into a heated argument over how Shrek and Fiona will raise their family, and Fiona, disgusted at Shrek's behavior, locks herself away in her room that evening. Shrek worries that he has lost his true love, particularly after finding her childhood diary and reading that she was once infatuated with Prince Charming (voiced by Rupert Everett). When Fiona realizes that Shrek left she asks her father for help but he replies that he always wanted the best for her and that she should better think about what is the best for her, too.
As everyone goes their separate ways, Harold is accosted by the Fairy Godmother (voiced by Jennifer Saunders) and Charming, her son. The two retell the Prince's adventures and how he overcame many obstacles and climbed a high tower in order to rescue her, but in vain. They reprimand Harold for breaking an old promise that Charming would be able to marry Fiona, and demand that he find a way to get rid of Shrek. Harold arranges for Shrek and Donkey to join him on a fictitious hunting trip, which really is a trap to lure the two into the hands of an assassin, Puss in Boots (voiced by Antonio Banderas). However, Puss is unable to defeat Shrek and, revealing that he was paid by Harold, asks to come along as a way to make amends. The three sneak into the Fairy Godmother's potion factory and steal a "Happily Ever After" potion that Shrek believes will restore Fiona's love for him.
Shrek and Donkey both drink the potion and fall into a deep sleep, awakening the next morning to discover its effects: Shrek is now a handsome human, while Donkey has turned into a stallion. In order to make the change permanent, Shrek must kiss Fiona by midnight. He, Donkey, and Puss return to the castle to discover that the potion has also transformed Fiona back into her former, human self. However, the Fairy Godmother, having learned of the potion's theft, intercepts Shrek and sends Charming to pose as him and win her love. At the Fairy Godmother's urging, Shrek leaves the castle, believing that the best way to make Fiona happy is to let her go.
To ensure that Fiona falls in love with Charming, the Fairy Godmother gives Harold a love potion to put into Fiona's tea. But Herold replies that it's not possible to make his daughter fall in love in this way. This exchange is overheard by Shrek, Donkey, and Puss, who are soon arrested by the royal guards and thrown into a dungeon. While the royal ball begins, several of Shrek's friends band together to free the trio and create a gigantic gingerbread man (whose name is Mongo), which breaks through the castle's defenses so Shrek can stop Charming from kissing Fiona. He is too late to stop them; instead of falling in love with Charming, though, Fiona knocks him out with a headbutt. Harold reveals that he never gave Fiona the love potion, whereupon the Fairy Godmother attacks Shrek. In the ensuing melee, a spell from her wand, presumably to kill Shrek, rebounds off Harold's armor, when he commits self-sacrifice to save Shrek, and disintegrates her; it also returns Harold to his true form, that of the Frog Prince. He had used the Happily Ever After potion years ago in order to marry Lillian, but she tells him that she loves him regardless of his appearance.
As the clock strikes midnight, Shrek and Fiona let the potion's effects wear off and they revert to their ogre selves, while Donkey changes back as well. Harold gives his blessing to the marriage and apologizes for his earlier behavior, and the party resumes as the credits begin. They are interrupted by a brief after-party scene in which the Dragon, who had romanced Donkey in the first movie, arrives and reveals that they now have several dragon-donkey hybrid, or "dronkey", babies.