Roglič attacked consistently in the last 50km of racing and then bombed the descent from the Col d'Aubisque to escape the clutches of the yellow jersey group of Geraint Thomas (Team Sky).
He finished ahead of a fast-moving Thomas with Romain Bardet (AG2R La Mondiale) third after a pulsating 200.5km of racing from Lourdes to Laruns.
"It’s crazy but it’s a very nice feeling," said Roglič. "I had great legs today. I really tried a lot of times. It was perfect.
"Going downhill, I opened a little gap and I knew it would be hard for anyone to close it, so I pushed harder and harder. When I heard I had ten seconds lead or something, just 5km before the finish, I gave it all till the end."
With the stage win, Roglič also improved his place in the general classification, moving above Chris Froome to sit third on the overall classification ahead of the penultimate stage time trial.
"I haven’t thought of the podium," said Roglič. "I was fighting for the stage win and it worked out very well. The time trial is tailor-made for me but it’s the same for everyone. Tomorrow it’ll be a new day and a new focus. I’m not bothered about Tom Dumoulin or Chris Froome. I’m bothered about myself."
Thomas managed to snag some bonus seconds at the finish to slightly extend his lead to two minutes and five seconds over Tom Dumoulin (Sunweb) with Roglič at two minutes and 24 seconds. Froome is fourth at two minutes and 37 seconds.
Dumoulin is the world time trial champion, and Roglič is also a powerhouse on the rise, but Thomas will be confident of defending his advantage on the upcoming 31 kilometre race against the clock which will settle the general classification fight.
The stage started with an 18-rider break which then became 12 by the time it reached the Col de Tourmalet where the stage came alive. Ilnur Zakarin (Katusha-Alpecin), Mikel Landa (Movistar), Rafal Majka (BORA-hansgrohe) and Bardet attacked halfway up the ascent while Alaphilippe (Quick-Step Floors), in the polka dots jersey, claimed the Souvenir Jacques-Goddet for being the first to the summit.
Alaphilippe had earlier made it mathematically impossible for any rider to overtake him in the mountains classification when he also took all the points on the Col d'Aspin.
Behind the front group, the general classification riders lower on the standing took the opportunity to launch their own attacks with Ilnur Zakarin, Romain Bardet, Jakob Fuglsang and Mikel Landa all trying their own solo bids to potentially bid for stage honours and move up the overall rankings.
The group of GC attackers joined the early break in the valley between the Col de Tourmalet and the Col de Borderes, forming a group of 11 riders out front. Team Sky seemed to have matters under control with five riders protecting Thomas' maillot jaune, but Landa had overtaken all but Thomas in the virtual general classification, with a lead on the road of three and a half minutes.
Robert Gesink (LottoNL-Jumbo) cut the leading group's advantage drastically on the Col de Borderes bringing the attackers' advantage down to a minute and thirty seconds.
The pace-making of Gesink set up an attack by Kruijswijk, launching off the front on the first half of the Aubisque to provide a launchpad for fourth-placed Roglič.
A series of attacks followed, thinning out the group of the yellow jersey, as out in front the lead group had been reduced to just Bardet, Zakarin, Rafal Majka (BORA-hansgrohe) and Zakarin.
Roglič attacked twice with 13km remaining. He brought Thomas and Dumoulin with him and caught his LottoNL-Jumbo team-mate Kruijswijk.
Under pressure from Roglič, Froome lost contact with the yellow jersey, leaving Thomas with no assistance 30km before the end.
Froome continued to fight and with 25 km to go, the defending champion came across with Sky teammate Egan Bernal and Dan Martin (UAE Team Emirates).
Majka crested the col d’Aubisque first but was brought back by the yellow jersey group with 19km to go. Roglič then accelerated on the downhill to create a gap with 8km to go and crossed the line 19 seconds ahead of Geraint Thomas.
The big loser on the day was Nairo Quintana, who slipped down to ninth overall after finishing seven minutes down on Roglič.