Serena overcomes bagel set to reach Madrid semis
Williams appeared to wear down against the scrappy Spaniard on the red clay court. After getting blanked in the second set, she fell behind 4-2 in the third.
But the defending champion rallied to break Medina Garrigues' serve twice to move on.
"It was definitely a battle," Williams said. "I obviously didn't do much in the second set and gave her confidence to play better."
Williams will next face either Sara Errani or Ekaterina Makarova.
Second-seeded Maria Sharapova had less trouble easing by Kaia Kanepi of Estonia 6-2, 6-4 to set up a semifinal with either Ana Ivanovic or Angelique Kerber.
Williams' entertaining match against an opponent she had rightly described as a "grinder" the day before took several momentum swings.
The 63rd-ranked Medina Garrigues has 10 career clay-court titles -- the most of any active player -- and her footwork and hustle against Williams showed why.
Williams was ahead 2-0 in the third set and with two break chances to take firm control of the match, but Medina Garrigues ran off four straight points to hold serve and soon enough the upset looked to be in the works.
But Williams regained command of her groundstrokes and a deep drive forced Medina Garrigues into an error on her return and lose serve.
The American then used another powerful shot to break Medina Garrigues again before holding serve at love to close out the match.
Williams said she was still at a loss as to why she lost concentration in that second set, which she considered "a reference for what not to do."
"I just wasn't really into it, my feet weren't moving," she said. "To change it I got up early (before the third set) and did some high knees, some stretching, anything I could to get my intensity back up to where it needs to be."
Williams improved her record to 29-2 on the year.
Also later, Rafael Nadal and Andy Murray had quarterfinal matches.