It's always been a dangerous match-up for Sharapova - she'd won four of their previous five meetings, but their last three matches were marathon three-setters - and from the beginning Safarova gave her trouble, jumping out to a 3-1 lead and, after getting broken back, sneaking the set out in a tie-break.
The second set was a similar story, Safarova racing out to a 3-0 lead, Sharapova grinding back to 3-all, and the two staying on serve the next few games. But with Sharapova serving 4-5, Safarova pounced, missing her first match point with a netted forehand but immediately bringing up match point No.2.
The No.13-seeded Safarova ripped one last forehand winner to finish Sharapova off, 7-6(3), 6-4.
"I feel like I had small openings, and I just wasn't able to play a good few points," Sharapova said. "I just wasn't able to keep that level up today. She was able to do that for a longer period of time. She was the much more aggressive player, took the time away from me, created her angles - and I didn't.
"That was the difference today, in my opinion. My opponent was at a much higher level than I was, and more consistently than I was, and that results in a bad day at the office. It was just a tough day."
"The last few times I played Maria we had really long battles. I remember they were really close, and I almost beat her in Stuttgart, but I always lost," Safarova said afterwards. "Yesterday I was really pumped, and really ready, and I just wanted to take this match. I was just excited to be there.
"It was a beautiful day, a beautiful day to play."
That short crosscourt forehand Safarova hit on match point was something she used all match.
"Well, you have got to use it," she said. "It's a different rotation, and players usually don't like it, so I was taking advantage of the forehand side. Obviously I had to keep serving strong and just go for it. But I also had to concentrate hard - she's such a fighter and can come back from any situation."
She's now into her third Grand Slam quarterfinal, having been a quarterfinalist at the Australian Open in 2007 (falling to Nicole Vaidisova) and a semifinalist at Wimbledon in 2014 (falling to Petra Kvitova).
Next up for Safarova here will be No.21-seeded WTA Rising Star Garbiñe Muguruza, who rallied from 2-0 down in the first set and 4-1 down in the second set to beat No.28 seed Flavia Pennetta, 6-3, 6-4.