The world No.1 triumphed 6-7 (7/9) 6-4 7-6 (7/4) 5-7 6-4 to end his Grand Slam finals heartache, having lost five of his last six majors.
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.
But the Serbian's resilience proved vital and he rallied to secure his seventh slam, while Federer will be left to wonder whether he will ever get a better chance to win an 18th slam.
"I would like to dedicate this title to my first coach Jelena Gencic," Djokovic said. "She passed away last year and this title is for her."
"This is the tournament I always dreamed of winning. The best tournament, the most valuable one."
Of Federer, Djokovic said: "He's a great role model, a great champion and I thank him for letting me win today.
"That's why he's been the most successful player ever because he comes up with his best shots under pressure."
Federer said: "It was a great final, I can't believe I made it to five [sets]. It wasn't looking good for a while.
"Definitely I felt the love out here again, I've enjoyed myself a lot. See you next year."