After two sharp climbs and several attacks in the final 10 kilometers (six miles), Andersen broke away with two miles to go to finish 15 seconds ahead of the pursuit pack.
“I’m just amazed,” said the Dane of his career best win. “It’s always hard to say I’m good enough until I have done it.
In a Stellar Tour for Slovenia, Luka Mezgec took the sprint for second and Simon Consonni finished third.
Peter Sagan was only fourth on a stage he was aiming for to close the gap in the points standings with green jersey wearer Sam Bennett.
Former ski jumper Roglic was 44 seconds ahead of Slovenian compatriot Tadej Pogacar, and 59 seconds ahead of reigning champion Egan Bernal.
The battle for the yellow jersey is sure to reignite on Sunday. A 174.5 kilometer (108 mile) trek from Lyon ends with a grueling climb of the Grand Colombier in the Jura mountains, conducive to creating time differences.
The Slovenia v Colombia match at the top of the overall standings will take place over two Category 1 summits before tackling an even more difficult final climb that stretches over 17 kilometers (10 miles).
Andersen’s victory was another tactical victory for the Sunweb team which made Marc Hirschi of Switzerland a star in the race. Hirschi won stage 12 solo.
“When you see this youngster (Hirschi) doing these amazing things, of course I’m inspired too,” Andersen said of his teammate.
Hirschi and fellow Sunweb rider Tiesj Benoot also led with breaking gusts in the dynamic final stages which helped set Andersen’s decisive move in place.
Sagan, chasing an eighth green jersey over the last nine laps, worked hard to close every move and didn’t have the speed to take second in the final meters.