The Swiss second seed's surprise reverse follows Tuesday's shock defeat for world number one Novak Djokovic to unseeded Bulgarian Grigor Dimitrov.
Federer, 31, was playing his first tournament since taking a break following his loss against Rafa Nadal at the Indian Wells Masters in March.
It was the 23-year-old Nishikori's biggest win since he beat Serb Djokovic in the semi-finals in Basel in 2011.
Photograph: Gonzalo Arroyo Moreno/Getty Images
Nadal checks Paire to advance in Madrid
Madrid Open: Dimitrov packs off Djokovic
Mint-green Federer looking sharp on competitive return
Djokovic breezes past Somdev at windy Miami
Del Potro ruled out of Madrid Open with virus