Out of action for six weeks, the Swiss third seed was tested for the first time by his longtime rival in a series which began in 2002. Since they began in Moscow nine years ago, Federer had won all 11 matches in straight sets.
Fellow 30-year-old Niemimen, a finalist two weeks ago in Stockholm, achieved a personal goal as he finally won a set off the Swiss, taking the second with two breaks of Federer's serve and despite double-faults on two of four set points.
It took a big effort in the third set for 16-time grand slam winner Federer to re-establish control. But he still needed three match points to end with a cross-court winner after just over 90 minutes.
"I'm really happy to go through, said Federer, the four-time tournament champion and holder. "The second set was really tough and Jarkko played very well.
"He was also great at the end of the third (set). I found it tough to get my rhythm, it's a result of not playing for six weeks. To stay at your highest level you have to play consistently and I didn't manage to do that all the time today."
Federer's hard-fought victory took him to 26 wins from his last 27 matches in Basel, where his only loss in that period came in the 2009 final to Novak Djokovic.
Federer is without a title since the first week in January and is hoping for a big season finish this week, next week in Paris and at the eight-man season wrapup in London from November 20th.
The field of leading seeds was reduced to Federer and Serbia's number one Djokovic as second seed Andy Murray withdrew with a right buttock muscle strain.
The world number three was replaced in the draw by Basel-born Marco Chiudinelli. Ironically, his wild card was withdrawn by the tour in order to give it to Murray at late notice last week but he went on to lose to Robin Haase 6-2, 7-6 (9/7).
Murray said he woke up around 3am on Tuesday with pain in his buttock.
"I was struggling to walk," said Murray. "I had trained twice on Monday and felt fine after that.
"It was a bit better later on Tuesday morning and I went to a pool for some exercises and had a light hit. But this morning (Wednesday) I knew it was still not good enough.
"I don't know how I did it or what it came from. I've never had anything like this before."
The Scot said his personal physio and doctor suspect the problem might be linked to the sciatic nerve.
Murray said he will travel to Paris on Thursday and take four or five days off with anti-inflammatory treatment in the hope of being fit for the final event of the regular ATP season starting on Monday, the Paris Masters.
Murray's pull-out was the second of the day after Serb sixth seed Janko Tipsarevic was unable to go on when he was trailing 5-1 in the first set against German Florian Mayer in their first round match.
Marcos Baghdatis of Cyprus joined Federer in the last eight as he beat Swiss Michael Lammer 7-6 (7/2), 6-7 (2/7), 6-3, taking nearly two and a quarter-hours to go through.