The Ecuadorian attacked his breakaway companions on the final climb of Penas Blancas with two kilometres to go and held on to the line to finish nine seconds ahead of Wilco Kelderman (BORA-Hansgrohe) in second and 24 seconds faster than Marc Soler (UAE Team Emirates) in third.
After a frustrating start to this year's Vuelta, the win was a visibly emotional moment for Carapaz as he punched his handlebars in celebration, moving up eight places in the general classification to 16th with hopes for a top-10 finish now more realistic.
Despite crashing with 50 kilometres to go, race leader Remco Evenepoel (QuickStep AlphaVinyl) managed to recover and threaten his rivals in the finale, his advantage at the top still unchanged with nine stages left.
"We came here with a goal and the circumstances weren’t in our favour," Carapaz said following the stage.
"We had the focus now on trying to win a stage, and I’m really happy with that.
"I’m very happy. Mostly because the feelings I hoped for from the start are back. I have to enjoy it. I knew I wasn’t coming with the best condition after crashing at home.
"When we arrived in Spain after the Netherlands, we hit the mountains and it was a bit complicated for me. I moved on mentally to focus on a stage win. I knew I could do it in La Vuelta. Other years, I came 2nd, 3rd… It’s the first time, so I’m moved. There are still many stages to keep trying. We have to make the most of our current level."
A very flat start to the 192.7 kilometre stage took 50 kilometres for the day's break to form, with a massive group of 32 riders including Carapaz, Jay Vine (Alpecin-Deceuninck), Kelderman, Soler and Alexey Lutsenko (Astana) escaping off the front.
A steady gap of around 3 minutes was established before the last 100 kilometres when QuickStep began to slow their pacing and the lead was almost ten and a half minutes as the break hit the short climb before the Penas Blancas.
Evenepoel's crash with 50 kilometres to go for the peloton saw the bunch slow down and then pick up the pace again as ahead the break began to splinter at the base of the final climb.
Élie Gesbert (Arkea-Samsic) shredded the leading group with two attacks, the second leaving just four riders out in front as he was joined by Carapaz, Kelderman and Marco Brenner (Team DSM) with 3 kilometres to go.
Carapaz then surged with two kilometres to the line, outlasting Kelderman to take the victory as Soler caught Brenner and Gesbert late to recover third.
Evenepoel led the GC group with a pace high enough to discourage any attacks, crossing the line seven and a half minutes down on Carapaz.
La Vuelta continues with Stage 13, a rolling stage from Ronda to Montilla that will finish on a tricky uphill section that may take the race outside the capabilities of the pure sprinters. Watch from 10.50pm AEST on SBS and SBS On Demand.