World number one Djokovic had headed into the semi-final riding a 13-match winning streak at the event and seeking a third straight title.
But Spanish fourth seed Nadal had other ideas and extended his perfect run on hardcourts this year to 9-0 with a 6-4 3-6 7-6 (7/2) victory over the top-seeded Serbian in a thrilling match lasting more than two and a half hours.
Raonic earlier edged compatriot Vasek Pospisil 6-4 1-6 7-6 (7/4) in two hours 16 minutes and will become the first Canadian man to break into the world's top 10 when the rankings are announced on Monday.
Both players had early break points in their first ATP World Tour meeting and it was Raonic who was finally able to convert, taking a 3-2 lead before closing out the first set.
Pospisil responded in the second set, though. He dropped just four points on serve and broke Raonic twice to level the match.
Raonic was twice two points from victory in the 10th and 11th games of the decider, but each time Pospisil showed his resolve and forced a tie-break. Mini breaks were traded before Raonic established a 6-4 lead and converted his first match point after forcing a volley error from the stranded Pospisil.
Nadal's win boosted his chances of finishing the year as the world number one, and he was understandably delighted with his display.
"I said yesterday [Friday] the only chance to win against Novak, the only tactic is to play very well," he said on www.atpworldtour.com. "To play very well, I have to play aggressive. If not, I cannot play very well on this kind of surface. And I did. I played a very high level tonight [Saturday], I think.
"I played with the right decisions in the important moments. Yes, the serve worked well during the whole night. And the shots from the baseline, I tried to stay very close to the baseline, go inside when I had the chance."
Nadal never looked back in the first set after breaking the Djokovic serve in game one on an uncharacteristic double fault.
The Serbian hit back in the second set, finding his rhythm from the baseline and breaking serve in game nine from 40-0 down and going on to serve out the set to force a decider.
The decider went to the tie-break and, after a couple of mini breaks, Nadal had forged into a 6-0 advantage and capitalised on his third match point.
For Djokovic it was a second straight loss to Nadal, following the Spaniard's five-set victory in the French Open semi-finals.
"It was very close match," the Serbian said. "So, I mean, there were very few points that decided the winner. Can't really pick one moment.
"I had my chances, he had his chances. I guess at the end he played better... Whenever we play against each other, it's always a thrilling match for the crowd to see.
"We are both competing at the high level. We both want to win these matches."