You are not permitted to download, save or email this image. Visit image gallery to purchase the image.
The defending champion was outshone at times in the Centre Court thriller and saved two match points deep into a gripping decider but he dredged his tank of mental and physical resilience to win 7-6(5) 1-6 7-6(4) 4-6 13-12(3).
Nothing could divide the players in an intoxicating climax played in a soccer-like atmosphere and the match was already the longest Wimbledon singles final ever when the club's new rule stipulating a deciding set tiebreak at 12-12 was deployed.
Both players looked out on their feet after nearly five hours of compelling combat but, as he did in the day's two other tiebreaks, Djokovic proved more steadfast.
A sublime backhand, one of his best of the match, put the top seed 6-3 ahead. There was then a moment of confusion as, on his first match point, Djokovic made a successful Hawkeye challenge on a ball that was called out but the point was replayed.
Federer then ballooned a forehand off the frame of his racket to end the match after four hours 57 minutes of unforgettable drama -- nine minutes longer than Federer's defeat by Rafael Nadal in the 2008 epic.
It was a crushing blow for Federer who struck 94 winners to Djokovic's 54 and for long periods eclipsed the man on the other side of the net. He has now lost three Wimbledon finals to Djokovic, having been beaten in back-to-back finals in 2014 and 2015.
But having served for the match at 8-7 in the fifth and been 40-15 ahead, this defeat will sting the most.
Djokovic, 32, is now level with Bjorn Borg in fourth place on the all-time list of men's winners at Wimbledon and he moved to four wins behind Federer on the all-time Grand Slam list.
"Roger is one of the greatest players of all time, who I respect a lot," Djokovic, who last lost to Federer four years ago and now leads their rivalry 26-22, said on court.
"Unfortunately one player has to lose and we both had our chances. It's unreal to be two match points down and come back."
Federer held serve to stay alive seven times in the deciding set but broke the Djokovic serve at 7-7 and victory seemed in the palm of his hand at 40-15 in the next game.
The crowd prepared to salute the oldest man to win a Grand Slam in the professional era but Djokovic showed incredible resolve and refused to accept defeat.
A forehand mistake cost Federer his first match point and then he watched in horror as Djokovic whipped a forehand winner across him as he approached the net.
Eight service holds then sent the match into a tiebreaker at 12-12, a rule brought in after last year's tournament.
Djokovic jumped ahead and, for once, Federer had run out of magic as he suffered one of his most heart-breaking defeats.
"It was a great match, it was long, it had everything. Novak, congratulations, man, that was crazy," Federer said.
Federer was the more threatening player in the opening set but paid for failing to convert the one break point of the set in the fourth game when he missed a bread-and-butter forehand.
He then led 5-3 in the tiebreak but a succession of loose strokes allowed Djokovic to snatch it.
After the intensity of the first set, the second was a let-down as Djokovic went AWOL, winning only 12 points.
The Serbian regained his focus but with Federer winning more than 80 percent of first-serve points there was a sense Djokovic was merely hanging on.
Federer had a set point when Djokovic served at 4-5 but again his failure to convert came back to bite him.
Djokovic led 5-1 in the ensuing tiebreak and although Federer hit back to 5-4, the top seed capitalised on more Federer errors to restore his one-set lead.
Just as in the second set Federer stormed two breaks clear in the fourth and although Djokovic finally broke his serve after nearly three hours on court, the Swiss held to love at 5-4 to send the match into the tumultuous decider.