Andreas Kron took the second stage of the Vuelta Espana on Sunday while Andrea Piccolo grabbed the overall lead.
The stage effectively had two finish lines, one atop Montjuic and one nine kilometres earlier.
After a downpour payed havoc with the opening team time trial round Barcelona on Saturday, and with more rain forecast on Sunday, organisers ruled before the start that times for the general classification would be taken at the foot of the tricky final climb.
Piccolo, an Italian, who rides for EF Education who were third on Saturday, was part of a long breakaway.
As rain began to fall again the riders entered Barcelona at the end of a 182km run from Mataro, the peloton eased off its pursuit.
That allowed Piccolo to reach the 9km mark with enough of an advantage to take the overall lead from compatriot Lorenzo Milesi of DSM, one of several riders who crashed during the stage.
Piccolo was swallowed by the pack a kilometre later. The leading contenders were then ambushed by Kron.
The Dane escaped up the wooded hillside to win, collecting both the climbing and winner's bonuses. He crossed the line making a theatrical gesture of astonishment.
Australian sprinter Kaden Groves led a small chasing group across the line in second while the leading contenders rolled in together after opting to play it safe.
bdu/hpa/pb/dj