Solo win for cyclist, De la Cruz in yellow
Late in the 174km run from San Sebastien to Bilbao Roglic made a series of bids on a hilly section to shake off his rivals and clinch victory by just a two-second margin.
Australia's Michael Matthews was second on the day heading up the chasing pack, while Spain's David de la Cruz held the overall leader's yellow jersey by a single second on Roglic.
Defending champion Alberto Contador both fell and suffered a puncture before fighting back into the field on a teammates' bike.
"It's going to be a tough night but at least we're still in the race," Contador said coming in at 1min 28sec but benefiting from the final kilometre fall amnesty.
The Spaniard sits just three seconds off the lead in 31st place with two stages remaining that can turn the Tour on its head.
On Friday, the 139.8km run from Bilbao to Eibar has six category climbs culminating in a summit finish, before Saturday's deciding time-trial.