On a Centre Court, with an atmosphere that felt at times more akin to football than tennis, Djokovic won 7-6 (7-5) 1-6 7-6 (7-4) 4-6 13-12 (7-3).
As the clock ticked to four hours 57 minutes, Federer hit a ball high to hand the Serb victory.
The world number one has won 16 Grand Slams - and four of the last five.
"It's quite unreal," Djokovic said after winning his fifth Wimbledon title.
Federer, who at 37 was chasing a record-equalling ninth Wimbledon singles title, added: "It was a great match, it was long, it had everything. Novak, congratulations, man, that was crazy."
A highly anticipated final between two of the sport's greats always had the potential to go the distance - and this did that and more.
With fans unable to watch at times, while leaping to their feet and chanting at others, a nerve-jangling final set turned this into a classic.
When Federer had two championship points at 8-7, Djokovic held his nerve to save both and then break back, eventually taking it to the new tie-break at 12-12.
The Serb - who for extended periods of the match had been second best - had won the match's previous two tie-breaks and he did so again, snatching victory when Federer scooped a return high.
The Swiss had been seeking to become the oldest Grand Slam champion of the Open era but instead found himself part of a different record as the match time surpassed Wimbledon's longest final - the four hours 48 minutes of play in 2008 as he lost to Rafael Nadal.
"Like 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," Federer said.
"Epic ending, so close, so many moments. Yeah, I mean, sure there's similarities [between this and 2008]. I'm the loser both times, so that's the only similarity I see."
The incredible fifth set lasted more than two hours - you could have fitted in two of Saturday's women's singles finals in the time of that set alone.