World number one Serena Williams saved three match points to reach the quarter-finals of the Madrid Open.
The American was on the brink of defeat when Victoria Azarenka went 6-5 and 40-0 up on serve in the deciding set.
But Williams fought back with two winners before the 25-year-old Belarusian served three straight double faults to surrender the game.
Williams, 33, then ran away with the tie-break to complete a 7-6 (7-5), 3-6, 7-6 (7-1) victory.
"It was intense," said Williams after her win, which took two hours 43 minutes. "I could have won, she could have won. I ended up winning and I don't know how."
Azarenka, a two-time finalist in Madrid, was unseeded following an injury-hit season.
Williams came back from 5-1 down to take a first-set tie-break 7-5 before Azarenka, who beat her opponent's sister Venus in the first round, broke twice in the second set to set up a decider.
Leading 6-5 and 40-0, Azarenka was within sight of her first win in nine attempts against the American, but Williams struck a forehand winner and an unreturnable backhand before Azarenka's meltdown.
Williams won 12 of the last 13 points in the match to extend her winning streak to 26 matches.
In the other third round matches, defending champion Maria Sharapova beat Caroline Garcia 6-2 4-6 7-5 to maintain her return to form.
Fourth-seed Petra Kvitova defeated Anastasia Pavlyuchenkova 6-1 6-4, Caroline Wozniacki crushed Agnieszka Radwanska 6-3 6-2 and Irina Begu beat Barbora Strycova 6-4 6-4.
Carla Suarez Navarro upset seventh seed Ana Ivanovic 7-5 1-6 6-4 and Lucie Safarova beat Roberta Vinci (Italy) 6-4 6-7 (6-8) 6-0.
Russia's Svetlana Kuznetsova was pushed all the way by Australian Samantha Stosur 5-7, 6-2, 7-6 (5).