Japan's Kei Nishikori caused the second major shock of the Madrid Open by knocking out defending champion Roger Federer in three sets, 6-4 1-6 6-2.