Pole Majka broke away from an 11-strong escape group on the final first category climb to Risoul and managed to hold off a charging Nibali to win the 177km Alpine stage from Grenoble.
It was a welcome relief for Majka having been reeled in and passed by Nibali on Friday's first stage in the Alps, but also for his Tinkoff-Saxo team after losing leader Alberto Contador to injury.
"This morning I spoke with my teammates because yesterday I was second and I spoke also with the team and (manager) Bjarne (Riise) and I said if I get in the breakaway I will win the stage," said the 24-year-old Majka.
"At the bottom of the final climb we had an advantage of 1min 10sec over the peloton and I attacked and I dropped everyone, (Joaquim) Rodriguez and the Cannondale rider (Alessandro De Marchi) and I tried to win alone.
"When the gap was 35sec with 2km left to the finish I kept my motivation and fought for the stage win."
Majka held on to win by 24sec to Nibali with 37-year-old Frenchman Jean-Christophe Peraud coming over the line just two seconds later.
Nibali had attacked his rivals in the overall standings inside the final 4km and as he rode away the true battle for the podium places really started to hot up.
Second placed Alejandro Valverde started to struggle around 2km from home, allowing young French pair Thibaut Pinto and Romain Bardet, as well as American Tejay Van Garderen, to gain time as they came in fifth through seventh.
Valverde rolled over the line in 10th and lost 30 seconds to Van Garderen and 34 seconds to the two Frenchmen. The Spanish veteran drifted out to 4:37 behind Nibali but the next four riders behind him -- Bardet, Pinot, Van Garderen and Peraud -- are all within 1:30.
"Nibali is the strongest but there's nothing in it between the rest of us," said Valverde.
After struggling with Friday's heat, as temperatures reached around 35C, Peraud said he'd got his strength back on Saturday.
"I rediscovered the feelings I had in the Vosges. It wasn't so hot and suffocating today and when there's air, I can breath," said the AG2R teammate of Bardet.
"With Romain we were supposed to attack on the final climb but Nibali beat us to it, so I went with him."
That was a smart move as it allowed him to gain time on the four riders directly above him.
Nibali had won the three previous uphill finishes in this race and when he started to close in on Majka, it looked like he might maintain a 100 percent record on the summits.
But he said that wasn't his aim.
"When I attacked I tried to gain time, I saw that with a 50-second lead it would be difficult to catch Majka for the victory," said the 29-year-old Italian.
"I tried to manage the situation and just put some time into Valverde and the other rivals.
"I felt good so I could try something and things went well."
The day had begun with a 17-man breakaway quite early on which had been reduced to 10 by the top of the second of three categorised climbs, the hors category Col d'Izoard -- the highest point in this year's race at 2,360m.
They had a gap to the peloton of around 3min then but the bunch closed in to just over a minute behind by the start of the final ascent to Risoul.
Another breakaway companion had got back on by then but immediately the attacks started, firstly from De Marchi and Rodriguez, but then Majka swept past everyone.
Unlike the previous day's stage when he and Leopold Konig were caught by Nibali, this time the Pole had the strength to go all the way to the end.