The 27-year-old had lost five of his last six Grand Slam finals and spurned two chances in the fourth set to seal the match as failed to serve out with the score at 5-3.
The Serb then missed a championship point on Federer's serve at 5-4, which allowed the Swiss to take the match to a deciding set.
However, Djokovic held his nerve in the fifth to win his first Grand Slam title of the year and the seventh of his career.
He told reporters: "I would be lying if I said it was not in my mind. Of course it started playing with my confidence and I had some doubts.
"Of course people told me things, trying to help, but I managed to overcome this and I was the one to find a way.
"Today was a huge test, a mental challenge. We pushed each other to the limit and I could not drop concentration throughout the whole match to win it. That's why it was extra special."
Djokovic will surpass Rafael Nadal to become world number one when the rankings are announced later today.