The defending champion saved two match points in the fifth set before winning the first deciding tie-break played in singles at the All England Club for a 7-6 (7-5) 1-6 7-6 (7-4) 4-6 13-12 (7-3) victory after four hours and 57 minutes.
It was the longest men’s singles final in Wimbledon history and brought Djokovic a fifth title, equalling Bjorn Borg and now just three behind Federer.
The Serbian is closing in on Federer’s all-time grand-slam record, too, with his 16th title leaving him four off the Swiss and only two adrift of Rafael Nadal.
“It was a huge relief in the end, honestly,” said Djokovic, who once again proved himself the master at winning the points that matter.
“These kind of matches, you work for, you live for, they give sense and they give value to every minute you spend on the court training and working to get yourself in this position and play the match with one of your greatest rivals of all time.
“It was probably the mentally most demanding match I was ever part of. I had the most physically demanding match against Nadal in the finals of Australia that went almost six hours. But mentally this was a different level, because of everything.
“I’m just obviously thrilled and overjoyed with emotions to be sitting here in front of you as a winner. It was one shot away from losing the match. This match had everything. It could have gone easily his way.
“In these kind of moments, I just try to never lose self-belief, just stay calm, just focus on trying to get the ball back, return, which wasn’t serving me very well today. But, in the most important moments, all three tie-breaks I guess, I found my best game.”
It was an extraordinary contest. The sustained quality was not the same as Federer’s final loss to Rafael Nadal in 2008, which this match passed as the longest title decider, but it had more plot twists than a feature-length thriller.
Federer, who won 15 more points in the match, was the better player for all of the first four sets, with Djokovic’s game strangely off colour apart from his serve, but played two poor tie-breaks either side of a set that his opponent barely turned up for.
Djokovic’s level dropped again in the fourth and, when it looked like he was finally taking control early in the fifth with a break of serve to lead 4-2, he was promptly broken back.
Federer seemed to be under more pressure but it was he who broke to lead 8-7 and at 40-15 he held two match points.
The crowd, who had been chanting Federer’s name throughout, thought this was the moment but the 37-year-old netted a forehand and then was passed at the net as Djokovic broke back.
Federer had two more chances to break at 11-11 but again could not take them and his nemesis on the day, the tie-break, came back to haunt him once again.
The deciding tie-break was introduced following last year’s long semi-final between Kevin Anderson and John Isner and had not been needed at all during the singles tournament until this match.
Djokovic got irked by the crowd’s support for opponent Roberto Bautista Agut in the semi-finals but here remained pointedly calm, the only flash of anger coming when he swiped at a chair with his racket late in the fifth set.
“When the crowd is chanting ‘Roger’ I hear ‘Novak’,” he said. “It sounds silly, but it is like that. I try to convince myself.”
After beating his other great rival Nadal in the semi-finals, this was agonisingly close to the perfect tournament for Federer, who turns 38 next month and may never get a better chance to win a 21st slam title.
It was his third final defeat here by Djokovic in six years after losses in 2014 and 2015, while the match brought flashbacks of two semi-finals between the pair at the US Open, the Serbian saving two match points on each occasion.
Federer was left kicking himself at the chances he could not take, saying: “I don’t know what I feel right now. I just feel like it’s such an incredible opportunity missed. I can’t believe it.”
He held himself together admirably at the end and, although clearly stung, he insisted he would not dwell on it for too long.
He said: “Similar to getting broken when serving for the match: take it on your chin, you move on. You try to forget, try to take the good things out of this match. There’s just tons of it. Similar to ‘08 maybe, I will look back at it and think, ‘Well, it’s not that bad after all’. For now it hurts, and it should, like every loss does here at Wimbledon.
“I think it’s a mindset. I’m very strong at being able to move on because I don’t want to be depressed about actually an amazing tennis match.”
Barbora Strycova and Su-Wei Hsieh overcame a lengthy wait to finally get their hands on the women’s doubles title at Wimbledon.
The Czech-Taiwanese pairing, seeded third, beat fourth seeds Gabriela Dabrowski and Yifan Xu 6-2 6-4 on Centre Court over 24 hours after they were due to play.
It completed a memorable fortnight for Strycova, who produced a career-best run in the singles, where she lost to Serena Williams in the semi-finals.
The match was originally due to get started on Saturday tea-time but no one accounted for the men’s doubles final going on for four hours and 56 minutes –just five minutes shy of the longest ever.
As a result of that, their match was postponed late on Saturday night and rescheduled second on Centre Court on Sunday, but they again were left twiddling their thumbs as Djokovic and Federer battled out the longest ever men’s singles finals.
Finally, at around 7.30pm on Sunday, the players made their way out on to the court and Strycova and Hsieh were in no mood to mess around, getting the job done in 67 minutes.
After the first four games were shared, the third seeds made their move, winning four successive games to take the first set.
The first three games in the second set were all breaks and two of them went to Strycova and Hsieh, putting them in front – a lead they were able to hold.