Former world number one Federer was the first to break in the opening set for a 3-1 lead, but Nishikori's response was a positive one as the Japanese player broke twice to go 4-3 up.
The unpredictability continued, though, as Federer forced deuce in the very next Nishikori service game, which he won when his 25-year-old opponent shot wide.
The next three services games were held, but when serving to stay in the set at 5-6 down, Nishikori was once again forced to 40-40. He handed Federer the opportunity to win the set with a backhand that crashed into the net and a similar shot during the next point sealed it for the man from Basel.
As the second set got underway it appeared that Federer, who had won the last three meetings between the pair, was going to extend that run to four in relatively easy fashion.
He took a 4-1 lead as Nishikori was made to work hard around the court, but just as he did during the first set, the 2014 US Open finalist rallied and won four games on the bounce, which in turn left him in a position to serve for the second set and he duly took it.
The deciding third set was a carbon copy of the one that had preceded it in that Federer, who saved three break points early on, raced 4-1 in front, only for Nishikori to respond to level up proceedings.
However, whereas previously Nishikori was able to keep up that momentum, on this occasion Federer held for a 5-4 lead and then having forced deuce, he converted his first match point with an overhead smash.
Thirty-four-year-old Federer headed into the encounter in the knowledge that he had already qualified for the last four at the O2 Arena, but in contrast the defeat has seen Nishikori exit the competition. body check tags ::