Federer beat Nadal in last year's final of the clay Masters Series event before going on to claim the French Open and Wimbledon crowns and snatching back the number one ranking.
The eagerly-anticipated rematch in a packed and rowdy Magic Box arena graced by Spanish Queen Sofia was their first meeting since the 2009 title match and sets Nadal up for a run at regaining his Roland Garros crown when the grand slam starts next Sunday.
It was the Spaniard's 14th win over Federer in 21 matches, and his 10th in 12 on clay, and gives the 23-year-old a record 18th Masters Series title, surpassing Andre Agassi.
He also becomes the first player to win all three Masters events on the red dust in one season after triumphs last month at Monte Carlo and Rome. He will regain number two in the rankings when they are updated on Monday.
"It's really incredible and I would never have imagined achieving what I have, I need time to let it all sink in," Nadal said at a news conference.
"Beating Roger is always a special occasion and winning in Madrid is amazing and a dream for me," he added.
ANXIOUS BUZZ
The Magic Box's sliding silver roof was open to the Madrid sky and fans and tournament helpers were packed into every available space to witness the clash of the iconic pair, who have won 18 of the past 20 grand slam titles between them.
With shouts of "Vamos Rafa!" ringing out and the occasional, almost apologetic, "Vamos Roger!", Nadal drew first blood with a break of serve to edge 2-1 ahead.
Federer broke back immediately but lost his serve again to fall 4-3 behind and Nadal brought the boisterous crowd to their feet when he took the first set, prompting prolonged chants of "Rafa! Rafa!".
They each grabbed two more breaks of serve in the second set before an anxious buzz of anticipation descended on the arena for the start of the tiebreak.
Federer carved open a 4-2 lead with a sumptuous drop shot but netted an attempt to repeat the trick on the next point and Nadal raced into a 6-4 lead.
A thunderous Federer forehand winner saved one match point but as the Swiss tried to force the pace on the second he played a forehand airshot.
Nadal, sporting a pirate look with his yellow headband and flowing locks, raised his arms to the sky before throwing himself face down on the clay and then flinging his sweatbands into the ecstatic crowd.
"I've played more beautiful match points than this one," a smiling Federer said at a news conference.
"That's what happens, we're on clay after all. I decided to take a chance and it didn't pay off.
"He was obviously coming in to the match with loads of confidence having done so well on clay this season so it was always going to be tough," he added.