Federer and Nadal last met in an Australian Open semi-final nearly two years ago and played their last final in Rome in 2013, both of which Nadal won.
The 34-year-old Swiss, world number three behind Novak Djokovic and Andy Murray, claimed the 88th title of his career.
The Basel trophy was his sixth of the season as he polished his record to 58-9.
“Rafa had a great week,” said Federer who finished with 12 aces and 44 winners.
“It was special for us to have had this match and I hope we play many more finals.
“To win this one is incredible, a match with Nadal is always special. It was very important for me.”
Federer’s victory was his 11th against Nadal, who still holds the upper hand with 23 wins.
The win in two hours in front of a charged-up crowd at the St Jakobshalle gaveFederer his first victory over the Spaniard since his triumph in the semi-finals of the 2012 Indian Wells Masters, ending a five-match winning streak by the Spaniard.
Nadal had won 14 of their previous finals, with Federer claiming six.
But the Swiss has now improved to 5-1 against Nadal on indoor surfaces.
“It’s still been a fantastic week for me. It’s my fourth time here and I finally reached the final,” said 29-year-old Nadal.
“It’s been a very emotional week also. I had to fight in a lot of my matches. But it’s been a very important time both now and for the future (season).
“I’ve had huge support all week — maybe a bit less today, but I understand,” he joked.
Federer got away to a good start at the venue where he served as a ballboy two decades ago.
The top seed earned a break for 3-2 and concluded the opener with a second break to take the lead after 37 minutes.
But Nadal, who has been making comebacks all week, was having none of it.
With Federer on a roll, the Swiss won the fifth game with three aces and fired his ninth ace to secure a 5-4 lead.
Nadal earned a break point in the next game and converted to shock the Swiss with a 6-5 lead. The Spanish third seed quickly levelled the match at a set each on the first of three set points.
Federer came alive just in time in the final set, breaking for 5-3 and serving out for victory a game later on his second match point.