The ninth seed powered to a 1-6 6-3 6-2 victory over the Dane, clubbing a blistering 43 winners to reach the last eight in the Spanish capital for the first time.
"I had a really slow start today, I don't know why," Williams said. "I was just a little sluggish, mentally I was fighting some demons, maybe.
"The first set was not me at all. I haven't been playing like that and I just thought I got do to better than this and I started just to try to play my game."
Wozniacki, who beat Williams for the first time in four attempts at the Sony Ericsson Open in Miami earlier this year, started strongly but lost control of the match in the second set.
The American broke Wozniacki's first service game in the third set and, with the sliding roof open over the sun-drenched Manolo Santana centre court, tightened her grip.
The 2009 runner-up in Madrid saved a first match point with a fizzing crosscourt shot but was helpless at the second as Williams wrong-footed her with another powerful drive.
The controversial, blue clay surface, however, did not seem to be a problem for Williams.
"This clay is a little slippery," she added. "I feel a little bit like an ice-skater out here, but actually I can ice-skate so it's not a bad thing. I think I can play on any court."
Williams will meet second seed
Current number one
"She is doing really well," Williams said of Sharapova, whom she has beaten in their last six meetings.
"She does have the upper hand going into tomorrow's match, she has the momentum after winning (in Stuttgart) last month. So I'm just going out there with nothing to lose."
Wozniacki, still seeking her first title in 2012, had few complaints at the result.
"Williams is a great opponent but to be honest I am just happy that my foot's feeling better, you know I hurt it in the first match," she said.
"I think I can take a lot of confidence from this match leading into the French Open, which is coming up soon."
The winner between Williams and Sharapova will meet either fifth seed
The 26-year-old Hradecka, who stunned third seed Petra Kvitova in the second round, beat Russia's