Second seed Federer, who had just returned to the tour after a two-month break, joins world No 1 Novak Djokovic in being eliminated early in the Spanish capital and his lack of match practice showed.
One break was enough for Nishikori to take the opening set 6-4, but Federer roared back in the second to take it 6-1.
However, with the momentum seeming to be with the 17-time Grand Slam champion, it was Nishikori who edged ahead when he broke to move 3-1 in the third.
"He was my idol actually, so to beat him, that was one of my goals for my tennis career," Nishikori told a press conference.
More From This Section
"That it happens on clay is amazing because, you know, it's not my really favourite surface. I feel good, of course. I wasn't really expecting this, it is not easy, 6-2 in the third, that's not easy against him."
Three-time champion Federer, who beat Nishikori in straight sets in their only other previous clash in Basel in 2011, admitted Nishikori was a deserving winner.
Rafael Nadal eased into the last eight with a 6-2 6-3 win over Mikhail Youzhny and will next face either Tommy Haas of Germany or Spanish compatriot David Ferrer.
The 26-year-old Nadal now stands at 28-2 on the season as he looks to reach his seventh final since his comeback from a seven-month injury lay-off in February.
"Since I've been back I haven't had the security that I am in my perfect moment or that I can give myself definite goals," said Nadal.
"I just to have try and feel that I am getting better and better day after day. I have to have my feet on the ground. I've made it to the quarter-finals of Madrid, which, for me, it's lovely to be able to say that today.