A day after his 19th birthday, Alcaraz, who has already broken into the top 10, defied an ankle injury suffered in a second-set fall to defeat his 35-year-old opponent 6-2 1-6 6-3.
It seemed the teenager might not be able to finish the match when he went over on his right ankle in the third game of the second set having played a brilliant opener.
Nadal reeled off five straight games, but Alcaraz began to move more freely again at the start of the decider and held on to an early break of serve to beat his idol.
Alcaraz had lost his two previous meetings with Nadal but, while this will inevitably be hailed as a changing-of-the-guard moment, there were mitigating factors for the 21-time grand slam champion, not least his gruelling clash on Thursday against David Goffin.
This was also Nadal’s first event since sustaining a stress fracture of a rib six weeks ago, while Madrid is his least successful of the big clay-court events.
Nadal will head to Rome to prepare for the Italian Open as he builds up to the French Open, while Alcaraz moves on to an intriguing first career meeting with Djokovic.
The Serbian has looked much more like his normal self this week and he produced a fine display to defeat Pole Hubert Hurkacz 6-3 6-4.
“I was serving really well in the second set, I think that kept me alive,” said the world No 1. “I was pleased with the way I was constructing points. I tried to make him run and miss and overall it was a solid performance.
“It’s definitely a positive indication that I am on the right track in terms of getting to the desired level of tennis, being in one of the best tournaments in the world where all the best players are playing. Of course it does give me confidence, and it encourages me to keep going.”
Djokovic received a third-round walkover after Andy Murray went down with food poisoning and the Scot has decided not to take his place in qualifying for the Italian Open.
He will decide early next week whether to play in the French Open, which he had previously said he would skip, or turn his attention to the grass-court season.
Meanwhile, world No 2 Daniil Medvedev, who has not played since the Miami Open in March after undergoing hernia surgery, will make his return at the Geneva Open beginning on May 14 and looks set to be fit for Roland Garros.