Geraint Thomas made history as the first Briton to win a Tour de France stage on Alpe d’Huez, extending his overall lead on stage 12.

At the top of one of cycling’s most revered climbs the Welshman won in an uphill sprint from Team Sunweb’s Tom Dumoulin and AG2R La Mondiale’s Romain Bardet, with his Sky team-mate Chris Froome fourth after being struck by one fan and apparently spat at by another on the Alpe.

Thomas also became the first man to win in Alpe d’Huez in yellow – with Lance Armstring’s 2004 time trial victory expunged from the record books – and the first rider to win back-to-back mountain stages on the Tour since Swiss Toni Rominger in 1993 after victory in La Rosiere on Wednesday.

“It’s cloud nine, mate,” Thomas said. “I cannot believe it, it’s just insane.

“Not even in my wildest dreams did I think I would win on here. It’s one of those things that will stay with me for the rest of my life.”

The drama on the road matched the colour and the spectacle at the side of it as battle unfolded in front of raucous crowds on the famed 21 hairpins.

By the end of the 175.5km stage from Bourg-Saint-Maurice in which Sky had to chase down a long-range attack from LottoNL-Jumbo’s Steven Kruiswijk, they had picked up time gaps on several rivals.

Movistar’s Nairo Quintana and Alejandro Valverde cracked on the climb along with UAE Team Emirates’ Dan Martin, while Bahrain-Merida’s Vincenzo Nibali hit the deck trying to weave his way through fans just millimetres from their heroes.

His team later announced he would leave the Tour as a result of the injuries he sustained, saying “examinations have shown the vertebral body fracture of the tenth thoracic vertebra.”

Thomas won by just a couple of seconds from Dumoulin, with Bardet one second behind him and Froome a further second behind again.

Together with Movistar’s Mikel Landa, the quartet had attacked and counter-attacked one another in the final kilometres but were back together by the end, with Thomas accelerating out of the last bend to take stage honours for a second straight day.

With bonus seconds applied, Thomas extended his lead in yellow over Froome by 14 seconds to one minute 39 seconds, but insisted the four-time Tour winner remained Team Sky’s leader in the race.

“Like I said yesterday, I’m still riding for Froomey,” Thomas said. “Froomey is still the man. He knows how to ride for three weeks. Legend gets used way too much but he’s probably the best ever so I’m just going to enjoy this.”

Dumoulin is third overall, one minute 50 seconds behind Thomas. Nibali was fourth at two minutes 37 seconds.

With some flatter days to come, Thomas could enjoy a few more days in the leader’s jersey to come.

“I just want to enjoy tomorrow,” he said. “It was a bit too hard to enjoy today.”

Though Froome saw his deficit to yellow grow, he called Thomas’ lead a “dream position” for Sky.

“He’s ridden the race of his life so far at this Tour,” Froome said. “It’s been faultless and he fully deserves to be in yellow, having won two stages and the most iconic stage of the race Alpe d’Huez. It’s a massive, massive feather in his cap.

“I think it’s a dream position for us to be in – first and second on GC. It allows us to play both our cards like we did today. Yesterday ‘G’ went up the road and left other riders scrambling to try and chase. Today I went up the road and ‘G’ sat on Dumoulin which worked out really well in the final. It meant ‘G’ had a good punch at the finish. It’s just a dream scenario for us right now.

Both Thomas and Sky’s sporting director Nico Portal indicated they had been trying to set up Froome for victory, but his attack inside the final four kilometres was quickly shut down by Dumoulin and Bardet.

Froome was slapped by one spectator – turning briefly to remonstrate – and apparently spat at by another as he faced some of the lingering ill-feeling from the salbutamol case in which he was cleared of wrongdoing days before the Tour began.

The 33-year-old, unusually, did not answer reporters’ questions after the stage, speaking only via the team’s website, but Thomas said: “If people don’t like Sky and want to boo, that’s fine. Boo all you like. But just let us race.

“Don’t affect the race, don’t touch the riders, don’t spit at us. Have a bit of decency.”

While Froome survived, 2014 Tour winner Nibali hit the deck as the leading group tried to weave their way through the thick crowds, almost taking Thomas with him.

The Italian finished just 13 seconds behind Thomas, but was then taken to hospital in Grenoble for scans on his back which ended his participation.

‘The Shark’ could yet be added to a lengthy casualty list on a day which saw the sprint field decimated.

Double-stage winners Fernando Gaviria and Dylan Groenewegen, plus Andre Greipel, were among six riders to abandon mid-stage, while Rein Taaramae crossed the line in tears as one of two to miss the time cut.

Rigoberto Uran, who finished second overall last year, did not start the stage as the effects of a stage nine crash caught up with him.