Soler (UAE Team Emirates XRG) led by more than a minute during the final 16.9km climb to La Farrapona in the 139.5km mountainous ride from Aviles, but Vingegaard (Visma-Lease a Bike) made a late push to finish 39 seconds behind the Spaniard.
Soler's teammate Joao Almeida followed Vingegaard to the finish line to stay second in the race for the red jersey, sitting 48 seconds overall behind the Dane.
"It's unbelievable. I cannot explain ... an unexpected day, but I'm very happy," said 31-year-old Soler after his first Vuelta stage win of the year.
"I knew the valley road afterwards up to Farrapona was a very hard one; if you get a gap there, then you can keep it. I was worried about the wind, but it turned out okay."
The stage was disrupted by pro-Palestinian protesters during the neutral start, while Israel-Premier Tech removed the team's full name from their riders' kits.
Briton James Shaw was the first to the top of San Lorenzo, the first category one summit in the race, launching a late attack during the 10.1km climb with an average gradient of 8.5%.
But it was Soler who took the lead ahead of the last climb, as Johannes Staune-Mittet tried to chase him down.
But the Spaniard was ahead by a minute and 34 seconds going into the final five km.
Vingegaard, who was closely followed by general classification contenders Almeida and Tom Pidcock for most of the stage, was happy to stay in a group nearly three minutes behind the two leaders until the last five km.
The Dane quickly reduced his gap with Soler as Almeida tried in vain to beat him to the finish line.
Pidcock remained third in the general classification, sitting two minutes and 38 seconds behind Vingegaard.
"I lost a bit more time, but I'm getting better and better at these longer efforts," Pidcock said.
The race continues on Sunday with a 167.8km ride from Vegadeo to Monforte de Lemos.