The Serb, 27, won 6-7 (7-9) 6-4 7-6 (7-4) 5-7 6-4 after missing a championship point and letting a 5-2 lead slip in the fourth set.
By winning the fifth set, Djokovic ended a run of three straight defeats - and five out of six - in major finals.
"It's the most special Grand Slam final I've played," he said.
"At this time of my career for this Grand Slam trophy to arrive is crucial, especially after losing several Grand Slam finals in a row.
Djokovic's ability to produce high-class returns at crucial stages in the match was key to his victory. A clear tactic he used was to return to the Federer backhand. The second-serve return was where Djokovic was most dominant, returning at an average speed of 68mph and winning 56% of those points. Federer returned Djokovic's second serve at an average speed of just 57mph and only won 35% of those points. With both players dominant on first serve, taking advantage of the second was vital.
"I started doubting a little bit. I needed this win a lot. I'm going to try to use it in the best possible way and for my confidence to grow for the rest of my season and the rest of my career."
Djokovic added a second Wimbledon title to his 2011 triumph and has now won seven Grand Slam titles, joining John McEnroe and Mats Wilander in the all-time list, led by Federer on 17.
He also moves past the six titles of Boris Becker, the man he brought in as head coach at the beginning of 2014.
"I am very relieved to win a Grand Slam with Boris," said Djokovic, having lost in the quarter-finals of this year's Australian Open and the final of the French Open.
"Many people doubted that decision, but I am sure it is a great combination."
Djokovic had been in danger of another painful defeat when Federer fought back with five games in a row in the fourth set, and paid tribute to the 32-year-old Swiss.
"Roger played very well, in a very high level. He showed why he's a champion. He showed a fighting spirit, composure in important moments when he was a break down."
Djokovic described the contest as the best of his 14 Grand Slam finals, ahead of his record-breaking win over Rafael Nadal at the 2012 Australian Open that took five hours and 53 minutes.
"This has been the best quality Grand Slam final that I have ever been part of," said Djokovic.
"I've had a longest final against Nadal in the Australian Open 2012, but, quality-wise from the first to last point, this is definitely the best match."
Djokovic will regain the world number one ranking from Nadal on Monday, but his focus will now shift to his fiancee, Jelena Ristic, and the imminent arrival of their first child.
"I'm going to take some rest," he said. "There are a few important things coming up. Getting married, and of course in a few months becoming a dad.
"I can close the chapter of my tennis career just for a little bit now. I deserve that for a few weeks - to rest, to enjoy, be with my fiancee, my wife to be, and my family."