Federer and Nadal produced one of the finest contests their rivalry has ever seen but it was the Swiss that prevailed, winning 6-4 3-6 6-1 3-6 6-3 after three hours and 38 minutes on Rod Laver Arena.
It means Federer has secured his fifth Melbourne crown and a first grand slam success since Wimbledon in 2012.
If this was to be the last grand slam duel between this pair, perhaps the two finest players the sport has ever seen, it was more than a fitting finale.
Nadal twice came back from a set down, and then led by a break in the fifth, but Federer drew on all his powers of brilliance to win one of the most unpredictable finals in recent memory.
Federer is now the first man in history to win five or more times at three different major events, and with 18 triumphs he extends his record as the most successful male player of all time.
His victory is also his first over Nadal at a grand slam since the Wimbledon final in 2007.
The 35-year-old's achievement is all the more remarkable given he arrived here having not played a single official match since Wimbledon, after taking the second half of 2016 off to recover from injury.
Federer had not expected to go past the fourth round but now he is the champion, capping a topsy-turvy tournament with arguably its greatest twist.
"I'm out of words," an emotional Federer said on court afterwards. "I'd like to congratulate Rafa on an amazing comeback.
"I'm happy for you. I would have been happy to lose too to be honest, the comeback was perfect as it was.
"Tennis is a tough sport. There are no draws but if there was going to be one tonight I would have been happy to share it with Rafa."
1️⃣8️⃣🏆 pic.twitter.com/Ap8aZGhBNf— Roger Federer (@rogerfederer) January 29, 2017
For Nadal, this is the third time he has lost in the Australian Open final, having won here once in 2009, but the 30-year-old has made a resurgence of his own and looks ready to return to his best.
"Congratulations to Roger and all his team," Nadal said.
"I fight a lot to be where I am today. Today was a great match and probably Roger deserved it a little bit more than me so I'm just gonna keep trying.
"I feel I am back at a very high level. I am gonna keep fighting for the whole season and keep trying to have this trophy with me."
The first set carried added significance given Federer had beaten Nadal only twice in 35 meetings after losing it, and never at a grand slam.
A ringing mobile phone cut the tension and, after a slow start, Federer began to dial in too, as three booming backhands gave him the break for 4-3 and he served out with an ace to move one set to the good.
Everyone stop right now .. take 6 months off. Come back. Win ..... can't believe what I watched. Congrats to my friend @rogerfederer— andyroddick (@andyroddick) January 29, 2017
Just as Federer assumed control, however, Nadal wrestled it back, breaking once in the second and then again for a 4-0 lead.
He was peppering Federer's backhand at every opportunity but it was the forehand that was creaking most and Nadal cruised through for one-set all.
Nadal could now smell his opponent's insecurity and when two more forehands hit the net it seemed the Spaniard was sure to take charge.
Instead, Federer dug deep to hold and after being pinned against the ropes, the Swiss suddenly came out swinging.
He broke Nadal in the very next game, held in 63 seconds and, with his backhand singing, he broke again for 5-1 and swiftly closed out the set.
Bewildered, Nadal had sent three rackets off to be restrung but into the fourth and the momentum swung again.
Perhaps Federer lost concentration because he wafted one short forehand wide, with the court at his mercy, and then framed another into the sky, allowing Nadal to break for 3-0. It was all Nadal needed to send the match to a decider.
Just as he had done in his semi-final against Stan Wawrinka, Federer left the court for a time-out, lasting almost seven minutes, for treatment on his leg.
Nadal had no intention of letting up. He broke in the first game of the fifth before staving off three break points with a series of stinging forehand winners.
Federer had the trainer on to massage his right thigh but he continued to pile the pressure on Nadal's serve and finally broke back at 3-2 when another backhand ripped cross-court left his opponent powerless to respond.
Back in the driving seat, Federer held for 4-3 with a second-serve ace and then opened up 0-40 on Nadal's serve when the Spaniard double-faulted.
Nadal battled back to deuce before Federer edged a pulsating 26-point rally, in which almost every shot appeared like a winner.
When a Nadal forehand missed, Federer finally converted his fifth break point, 11th for the set, to move up 5-3 and serve for the match.
Nadal set nerves jangling when he had 0-30 but Federer clawed back to deuce. He had one match point but sent a forehand long before an ace out wide gave Federer a second match point.
He blasted off a forehand winner but before the cheers could start, Nadal challenged the call and the crowd held their breath. The review showed the shot had nicked the line and Federer's victory was complete.