Roglic started Friday's 173.2 kilometre ride through Rioja from Logrono five seconds behind Ben O'Connor.
With 6km left on the closing climb to the Alto de Moncalvillo, Roglic and two of his Red Bull-Bora-hansgrohe team-mates blasted off the front.
The Slovenian said at the finish that he had not been keen on the tactic.
"I didn't need the stage but some guys decided they would pull for me so we went for it," he said.
Dani Martinez set the initial pace. Aleksandr Vlasov briefly took over before their team leader zoomed away from both.
Roglic crossed the line 46 seconds ahead of Frenchman David Gaudu of EF Education and Dane Mattias Skjelmose of Lidl.
Roglic won on the same ascent in 2020 on his way to his second overall victory in the race.
"I had some really nice memories from this climb and it didn't disappoint me again. Beautiful," he said.
O'Connor, of Decathlon AG2R, battled in one minute and 49 seconds behind as his 13-day reign in red came to an end.
"I was a bit broken at the end there," said O'Connor, saying he had been expecting another Bora attack. "I wasn't surprised but I didn't expect myself to be so bad. Stage 19, I guess."
The bonus Roglic gained for winning the stage meant he leads O'Connor by 1min 54sec. Spaniard Enric Mas is third a further 26sec back.
After his 15th Vuelta stage win, Roglic is two tough days away from a fourth Vuelta, which would equal the record for most victories held by Spaniard Roberto Heras.
Saturday's stage is another mountain stage, a 172km ride from Villarcayo to Picon Blanco before ending with a 24.6km time trial round Madrid, which ensures the final day is competitive rather than a victory parade.
The Slovenian insisted that the two coming days were the most important.
"The big queen stage is still to come tomorrow," he said "We don't do the normal laps in Madrid, we do a time trial, so it's far from done."
Roglic had lost the red jersey on stage six when O'Connor turned a long breakaway into a lead of more than five minutes.
"My gap is better than five minutes behind," said Roglic. "I'm happy with how I am functioning and I'm happy with the guys."
Meanwhile, O'Connor said there was still hope.
"There's still tomorrow and two more big days," before adding that he was looking beyond the end of the race.
"Monday and having beers and sitting on the terrace and relaxing," he said.