Djokovic wasted a 5-2 lead in the fourth set but held on for a 6-7 (7), 6-4, 7-6 (4), 5-7, 6-4 victory that returns the Serbian player to the No. 1 ranking.
"It was a great match to be a part of," Djokovic said in a postmatch interview. "[Roger's] a magnificent champion."
Djokovic had a match point at 5-4 in the fourth set, but Federer saved that with an ace and forced a fifth set.
"After dropping the fourth set, it wasn't easy to regroup, but I had to find the necessary energy," Djokovic said. "This is the tournament that I always dream of winning."
In the last set, Djokovic broke with the help of four mistakes by Federer to seal the win.
"Going into a match with Novak is always going to be tough," Federer said. "... I can only say congratulations today. ... Well deserved."
The Associated Press contributed to this report.