Djokovic (right) made Federer fight hard for his win
Federer's battling victory over Djokovic means that he will reclaim the number two spot in the world rankings from the Serb.
Twice a winner in Canada, the Swiss star raced through the first set in just 25 minutes and then went 2-0 up in the second.
But he suddenly lost his way and Djokovic seized his chance, breaking Federer twice to send the match into a decider.
The third set swayed back and forth and appeared headed for another tiebreak until Federer broke Djokovic at 6-5 clinching victory on his first match point.
"I came out with all guns blazing, I played fantastic in the first set but in the second Novak was able to raise his game and make it super competitive," Federer said.
"He fought bravely. He put his foot in the door before I could close it, but I'm very happy to be back in the final."
Djokovic said: "It's a little bit frustrating that I haven't used the
opportunity I had in the end. But, you know, he was the better player today."