Roglic finished the 33.7-kilometre 13th stage in 46 minutes and 39 seconds, crossing the line one second ahead of American rival William Barta.
"It's a nice victory because it's been a long time since I last won a time trial," Roglic said.
"Surprisingly, I felt strong. I expected to suffer a lot more. Many people also expected me to lose time from Carapaz up the hill but we all started from scratch with the same chances."
Carapaz finished seventh, 49 seconds off the pace and, despite falling 39 seconds behind Roglic in the overall standings, remains upbeat about his chances.
"It was a very tough TT and I’m very happy with how it went," the Ecuadorian said. "We’re here to try and get the Vuelta and we’re still on the road to that."
Hugh Carthy, meanwhile, is fourth in the time trial and third overall, 47 seconds behind Roglic.
"I'm happy with what I've done, but there are four more important days to come," Carthy told a Spanish radio reporter.
Australian Alexander Edmondson of Mitchelton-Scott finished 18th, 1:45 secs behind the leader.
After a rest day on Tuesday, riders faced a mostly flat seashore route before having to change bikes for a final climb of nearly 1.8 kilometres.
"It was up and down the whole day," Barta said. "I tried to over-pace the little climbs, recover on the downhills and save a little bit for the end because it's a really hard climb."
On Thursday, riders face a 204-kilometre 14th stage through the hilly Galician terrain.
The three-week race, being held amid tight health restrictions because of the coronavirus pandemic, finishes on Monday in Madrid.
WATCH Stage 14 of the Vuelta a Espana LIVE, FREE and in HD on SBS VICELAND and On Demand tonight from 12:40am.