Djokovic served for the match in the fourth set and had a match point on Federer's serve but each time the 32-year-old Swiss player hung on.
Djokovic, who had lost five of his previous six grand slam finals, was staring at a shattering defeat but he simply refused to be beaten and took his second match point to win 6-7 (7/9) 6-4 7-6 (7/4) 5-7 6-4.
Loading article content
It was the Serbian's seventh grand slam title and his second Wimbledon triumph, while Federer will be left to wonder whether he will ever get a better chance to win an 18th slam.
The 32-year-old had to fight toe to toe off the ground in the 10th game, successfully repelling a Djokovic charge to the delight of his adoring public.
If there was mental anguish, Djokovic had buried it away, and it was only Federer's serve that kept the man from Basle on level terms.
Federer hung on for the tie-break and took advantage of two Djokovic errors to move into a 3-0 lead.
But the Serbian fought back to level at 4-4, and a forehand dumped into the net by Federer gave Djokovic a first set point on his own serve.
He could not take it, though, Federer finding the sideline with a forehand and forcing the error, and the Swiss saved a second set point with an ace.
Another big serve gave him a first set point and Djokovic could not counter, finding the net with a backhand.
The guest list for the Royal Box befitted the occasion, with the Duke and Duchess of Cambridge, David and Victoria Beckham and Hollywood star Samuel L Jackson joining various tennis greats.
The presence of Sir Chris Hoy and, in the public seats, Sir Alex Ferguson was surely an indication that the Scots had hoped for a different man to be in the final.
Not that anyone present could complain about the tennis on show.
Having missed his chance in the opening set, Djokovic pressed at the start of the second but Federer saved the first two break points of the match.
There was also concern for Djokovic, who had slipped numerous times against Dimitrov, when he took his first fall of the final. It was a heavy one, and the pain on his face was evident.
By the time the trainer arrived to have a look at his left ankle, the Serbian had made the breakthrough, sweeping a backhand pass beyond Federer, who was rushing to the net at every opportunity.
It was only the second time in the tournament that Federer's serve had been broken.
He could make no real impression on Djokovic's until the top seed served for the set, when a nervy forehand gave Federer his first break point.
But Djokovic's serve came to the rescue and he clinched the set 6-4 with a smash to level the match.
This was the 35th meeting between Federer and Djokovic but only their second grand slam final - the first came back in 2007 at the US Open - and second match at Wimbledon.
Federer won both of those matches, and the third set appeared crucial to his hopes of a repeat.
He was unlucky in the sixth game when he was robbed of 0-30 by an erroneous call by a linesman, with Djokovic going on to hold.
Federer's serving was almost immaculate and there may have been a first for the world's best returner in the ninth game as four consecutive aces flew past him.
He thought he had made it 6-5 with an ace but Djokovic challenged, the serve was shown to be wide, and the Serbian then forced a break point.
Federer saved that with a serve right onto the line, Djokovic screaming at the chalk dust in frustration, and another big serve countered a second break point before he ended the game with two more aces.
A second tie-break would decide it, and it was Djokovic who made the first move with a mini-break for 3-2, a forehand pass just clipping the top of the net.
But Federer fought back and a forehand he challenged more in hope than expectation was shown to be a millimetre in.
A forehand pulled wide handed the initiative back to Djokovic and gave him two serves to take the set 7-4, which he seized as Federer sliced a backhand wide.
Djokovic had been exceptional off the ground throughout the match, keeping Federer pushed back and finding the lines with impressive regularity.
He had a chance to really seize control as Federer slipped to 0-40 in the fourth game of the fourth set. The Swiss saved all three break points but a fourth followed and this time Federer went long.
Djokovic clenched his fist, a look of steely-eyed determination in his eyes.
Federer refused to give in, and out of nowhere responded with his first break of serve, leaping in the air as a forehand left Djokovic grasping at thin air.
The crowd's celebrations were silenced when their man dropped serve again immediately.
Surely that was that, but remarkably Federer broke Djokovic again when he served for the match, the top seed ending the game sprawled on the grass.
Still it looked like it would not be enough as Djokovic brought up a first match point, but Federer, the man who loathes Hawk-Eye, may revise his opinion after a challenge gave him an ace.
And for the first time Djokovic cracked, handing Federer three break points in the 11th game.
He took the third and then served out one of the most remarkable sets of his long career 7-5, winning his fifth game in a row.
The momentum was all with Federer, and physically Djokovic was struggling, too, taking a medical time-out after the third game of the fifth set for treatment to his right leg.
Federer piled on the pressure in the seventh game and brought up a break point but then netted a backhand.
Djokovic held and the match had swung towards him as Federer faced three break points but each time the Swiss came up with an answer.
The third, when he dug a half-volley off his toes, was one of the shots of the championship.
But Djokovic was pressing hard and two games later he brought up two match points.
This time Federer could not fight back, the Swiss netting a backhand as Djokovic celebrated a hugely emotional victory after three hours and 57 minutes.