Rumpelstiltskin's contract takes a day from Shrek's past, and he chooses the day Shrek was born. If the contract is not broken, Shrek will vanish at the end of the day. In the altered timeline Shrek was never born, so he never rescued Fiona, and Rumpelstiltskin has taken over the kingdom. The ogres are hunted by witches, and Fiona leads a resistance.
Shrek finds that his friends do not know him. He sets out to win Fiona's love, because "true love's kiss" will break the contract. He allies with Fiona, Donkey and Puss in Boots, and he surrenders himself to Rumpelstiltskin to save the ogres. Fiona's love is real but comes too late to break the spell by the deadline. Shrek vanishes, then the clock runs out and the world is restored.
He wakes in his normal life, now grateful for it. The closing lines have him telling Fiona that he always thought he rescued her from the dragon's keep, which reframes the story as one in which each of them rescued the other.