The top seed had saved two match points when Federer served for victory at 8-7 in the deciding set.
He went on to take it 7-6 (5) 1-6 7-6 (4) 4-6 13-12 (3) after an energy-sapping four hours and 57 minutes.
Federer had hoped to follow up his emotional semi-final victory over Rafael Nadal by completing the double over his other great rival for the first time at a grand slam.
He fought back well after he was twice set down but this will be one of his most painful losses.
The victory means the Serbian has now won four out of the last five major tournaments.
It takes his overall tally to 16 and making Federer's record of 20 seem ever more reachable.
This final has been dominated by its historical importance with 37-year-old Federer staring at perhaps his last chance to put further distance between himself and his younger rivals.
Nadal is now just two adrift and shows no sign of letting his iron grip on the French Open slip, while Djokovic has been open about the fact he is playing for history.
After his victory, Djokovic said: "Roger is one of the greatest players of all-time, who I respect a lot. He inspires me, for sure."
Federer said the final had been "crazy" and added: "I will try to forget this final but it was a great match!
"It was long and had everything. I had my chances, but so did he. I have to be happy with my performance. But Novak, congratulations, that was crazy.
"I hope I give some other people a chance to believe around the world. I gave it all I had and I can still stand. I hope the same can be said for all the other 37 year olds.
"My children won't be excited with the plate, they would be more excited with that golden thing. But it is all good, back to being a dad and a husband.
Monday 15 July 2019 08:56