Djokovic, ranked number three in the world, defied the blustery conditions to beat the world number two 3-6, 6-2, 6-3 in one hour and 46 minutes.
Federer, the 13-time Grand Slam champion, who is in search of his first title of 2009, comfortably won the first set as Djokovic found his feet, but could find no answers after that - finishing with 35 unforced errors.
"Novak played bad at the beginning, and I finished worse than him," said Federer, who lost seven straight games bridging the second and third sets.
That span included four breaks of Federer's serve, after he had had surrendered his serve just once in the tournament and contributed to a rare glimpse of temper from the normally restrained Swiss, who broke a racquet after yet another forehand miss in the third game of the final set.
"You try hard and then it's just not working," Federer said.
"Today it is different just because there's so much wind as well. Once you start feeling bad, it's kind of tough to regroup."
Djokovic said the windy weather and some early nerves both played a role in his shaky start.
Big-serving Del Potro, who came back from 3-0 down in the third set, will face Britain's Andy Murray for a place in the final.
The tall Argentine takes his place in the last four of a Masters event for the first time with the crowd-pleasing victory. It was also his first win over Nadal at the fifth attempt.
"Maybe it was wonderful for the crowd but it was terrible for me," said Nadal.
"I think I played really bad all the time. That's the truth of this match. Later when I was 3-0 up in the third I played worse.
"I had the match; it was an amazing disaster."
In an atmosphere more akin to a football match, Del Potro, who lost to Nadal in Indian Wells last month, was roared on by hundreds of Argentinian fans who sensed an upset from the moment their man broke in the first set to go 5-4 up.
Having served out to win the opening set, Del Potro broke early in the second but the 20-year-old from Tandil then faced the inevitable fightback from the Spaniard, who played some powerful strokes around the court to win the second.
Nadal looked on course for victory when he broke twice early in the third to go 3-0 up but with the fans chanting his name, Del Potro, looking wild in his sleeveless shirt and headband, found the strength to mount a revival of his own.
The Argentine broke back twice to go to 3-3, levelling the set with a brilliant cross-court drive.
Both players then stuck largely to the baseline, but at 6-5 to Del Potro, Nadal had to serve to save the match and found himself 40-15 down after going to the net and putting the ball out wide to the right.
Nadal saved three match points, with an ace bringing him back to deuce, and when Del Potro hit the net and then drove long the set was tied at 6-6.
Luck seemed to be with Nadal when at 2-2 in the tie-break his shot hit the top of the net and dropped down for a point, but once again Del Potro, celebrating every point with a fist pump, found another burst of determination and took the next five points to win.
Spaniard Nadal broke service twice in the first set to take it 7-5 and, after the determined and aggressive Gil broke early to go 2-0 up in the second, Nadal took control to close it out 6-3.
Nadal will face the winner of the third-round tie between Russian Igor Andreev and Switzerland's Stanislas Wawrinka.
Nadal's powerful serve was the basis of his victory - he produced five aces and won 76 per cent of points on first service.
Sixth-seeded Argentine Juan Martin del Potro secured his place in the next round with a 6-4 6-1 victory over Serbia's Janko Tipsarevic.
The impressive 20-year-old won 92 per cent of points on his first serve and converted four of five break points for victory.
Another Serb Viktor Troicki dealt with German lucky loser Bjorn Phau 6-4 6-3 to reach the fourth round at Key Biscayne for the second year in a row.