World number one Nadal came from behind to defeat Djokovic
Rafael Nadal saved three match points as he beat Novak Djokovic 3-6 7-6 (7-5) 7-6 (11-9) to set up a Madrid Open final clash with Roger Federer.
Djokovic came close to inflicting only Nadal's fifth loss on clay in 154 matches and capitalised on Nadal's sluggish start to take the first set.
Nadal's form improved after his knee was heavily strapped and he drew level after winning the second set tie-break.
Despite trailing 3-1 and having to save those match points he took the decider.
It was gripping stuff at Madrid's new Magic Box stadium and at four hours and three minutes it was the longest three-set Masters Series match in history.
"In a game like that with so many match points I think you need luck to win, there's no doubt about it, but I played with great courage and I think everything went well for me," said world number one Nadal, who has now won all nine of his clay meetings with Djokovic.
"I played the first set poorly, and it looked very bleak for me. But I think that little by little I was able to get my rhythm and ended up playing well.
"What I'm worried about is recovering in time and playing well. If I don't, I won't win."
Third seed Djokovic's match points came at 6-5, 7-6 and 9-8 in the final set tie-break, but each time Nadal held his nerve to pull level.
"It's happened too often," said Djokovic, who has been beaten by Nadal in the Monte Carlo and Rome Masters finals in recent weeks. "It's very disappointing to play as well as I have and still lose the match. I played one of my best matches ever.
"I was a couple of points from the victory. I even played a few points above my limits and I still didn't win."
The Spaniard, who claimed his 33rd straight win on clay, will now meet world number two Federer, who easily despatched Juan Martin del Potro 6-3 6-4.
It will be the first time the pair have met since the Australian Open final when the Swiss player was in tears after the defeat.
Federer has lost his last five matches against Nadal, including the finals at the French Open and Wimbledon as well as that Australian Open defeat.
"Playing Nadal anywhere is a challenge, especially on clay. The extra factor here is that this is in Spain," said Federer.
"This has never happened before. I'm excited to play him. We've not played so often (this year), the Australian Open was the last time. Now we are on clay, his surface. We'll see what happens."
The 13-time Grand Slam winner is still seeking his first title of the season.