Andy Murray and Rafael Nadal both advanced, but the defending Madrid champion Federer suffered an upset that left the tournament without its top two seeds. No. 1 Novak Djokovic was ousted earlier in the event.
Nishikori, seeded 14th, knocked out the former world No. 1 in 95 minutes 6-4, 1-6, 6-2.
"He was my idol and to beat him was one of the goals for my tennis career," Nishikori said. "Beating Roger is, yeah ... I need a couple of days to celebrate."
"He was the better player today for sure," the second-seeded Federer said. "I was lacking control from the baseline and that carried through from start to finish. Overall I'm disappointed with my play."
Nadal, seeded fifth and holding a 28-2 match record this season, coasted past Mikhail Youzhny 6-2, 6-3. Nadal is seeking his fifth tournament victory of the year and will try to move another step closer to it when he meets David Ferrer in the quarterfinals.
Ferrer ousted Tommy Haas 7-5, 4-6, 6-4.
The other quarterfinal will send Jo-Wilfried Tsonga against Stanislas Wawrinka.
Tsonga rallied past Fernando Verdasco 4-6, 6-3, 6-2 while Wawrinka won over Grigor Dimitrov 3-6, 6-4, 6-1. Dimitrov had shocked Djokovic in the second round.