The Movistar neo-pro rode away from the peloton of favourites 2km before the top of the climb to cross the line seven seconds ahead of Davide Formolo (BORA-hansgrohe) and overall contender Thibaut Pinot (Groupama-FDJ) who collected four seconds in time bonuses when he took third place.
“Because of the rain, people were a bit nervous at the beginning," Carapaz said. "It made the last climb a bit complicated but it was a good one for me.
"Once I got a ten seconds lead, I kept it. It’s a surprise for everyone. But I had very good legs and I followed the advice of the coach who asked me to try. I’ve managed to time it well.
"Sky was controlling things and AG2R had accelerated. After I seized the opportunity, I had to be cautious of not falling on the wet road."
There was an early break of eight, later shortened to seven, which held a time gap of five minutes before the peloton began to reel them in.
Their advantage stood at just over two minutes as they started up the 15km climb to the finish, with the rain lashing down, and they were eventually caught.
Koen Bouwman (LottoNL-Jumbo), who attacked from the breakaway early in the climb, was the last to be swept up as Carapaz passed him in the final kilometre.
Simon Yates (Mitchelton-Scott) retained the Maglia Rosa as he kept the race under control. He continued to lead defending champion Tom Dumoulin (Sunweb) and team-mate Esteban Chaves on a general classification that saw some shifts in position.
“It was a more difficult stage than I expected because of the rain but no drama," Yates said. "We rode a nice tempo, safely.
"I tried to get some bonus but I got boxed in. It was a really fast climb. It was hard to make a difference. I expect more of a selection tomorrow. I had really good feelings again today."
Pinot moved one position up with the time bonus to leapfrog Dominico Pozzovivo (Bahrain-Merida) while Chris Froome (Sky) fell on the final climb and lost his position to the stage winner, Carapaz, to lie in ninth place overall.
“It’s never much fun crashing in the final of a race, especially on a hilltop finish, but the roads were really slippery and I just lost my back wheel when I went over a white line accelerating out of the corner," Froome said.
“I didn’t want to be caught off guard coming into that final, so I think it was the right thing to do, to get back to the front and stay in control of things. I think the guys did a really good job of getting me back up there."
For Carapaz, already in the white of the best young rider, the stage was a series of firsts for the 24-year-old Ecuadorian.
"It means a lot to me. It’s the fruit of years of hard work since I was a kid. To win against so many favourites makes me extremely happy. It’s a result for my whole country," he said.
"I’m a bit sad that there’s no cycling in Ecuador. It’s lamentable. We get no support. I hope this victory will open doors to other kids who will come after me and also have a lot of talent.
"Many things came to my mind when I crossed the line. It’s the first time that an Ecuadorian cyclist wins at this level. I’m also the first Ecuadorian who's participated in the Giro.
"I came with the mentality of doing well. This victory will stay in my heart, it’s incredible. I came to the Giro to discover what it’s like and what I can do in cycling.
"Rome is still a long way away and there can be other surprises. I’ll try to keep the Maglia Bianca as long as possible. It would be a beautiful triumph.”