Almeida was the fastest of the elite climbers after also being the rider to force clear a select group in the final two kilometres of the summit finish at the Volta a Catalunya. He stuck to the inside line through the final corner and had the pace to overhaul Quintana just before the finish line, taking the mountain-top victory.
"I’m super happy. I’ve been working hard for so long," said Almeida. "This year I have not been so lucky, today was a bit unlucky, I had a crash early on, but I felt really good and I’m very happy with the win.
"I was feeling pretty good in the last climb, I was always controlling my effort. Then George (Bennett) was ahead, which was good, we just had to follow the wheels. Then I just did my pace and in the end, I sprinted for the victory.
"We had multiple riders to win or the other possibilities. We played our cards and we won."
166.7-kilometre stage was expected to be a decisive one in the battle for overall honours in the Volta a Catalunya, the last remaining summit finish for the race to Boí Taüll (13km at 6 per cent).
There was a battle for the teams to get their riders in the early move with the attackers eventually going clear on the first categorised climb of the day, the Coll de Boixols.
Marc Soler (UAE Team Emirates), Hugh Carthy (EF-Nippo), Bruno Armirail (Groupama-FDJ), Mark Donovan (Team DSM), Jesus Herrada (Cofidis), Mikel Bizkarra (Euskatel-Euskadi) and Juan Pedro Lopez (Trek-Segafredo).
Donovan and Armirail attacked with 23 kilometres remaining in the race, gapping their previous companions as the peloton closed in on the escapees at the foot of the final climb. Once the road tilted upwards, Armirail attacked to go solo.
Arkea-Samsic came to the front of the peloton to set-up leader Nairo Quintana for a potential stage win, stringing out the bunch quickly. It resulted in a brief accidental attack by race leader O'Connor, as Elie Gesbert (Arkea-Samsic) set a very hard tempo which saw the majority of the peloton apart from O'Connor unable to follow.
O'Connor was recovered by the other elite climbers and the confusion allowed George Bennett (UAE Team Emirates) to attack up the road, catching Armirail, though the Frenchman was able to hang on as Bennett drove the pace.
Jonothan Castroviejo (INEOS Grenadiers) set a hard pace in pursuit of Bennett, who was a danger on the general classification, with O'Connor isolated in the front group.
The elite group of climbers caught Bennett and Armirail, wth attacks then flowing thick and fast from there to the finish. A furious pace set by João Almeida proved to be the moment that created the decisive split, with O'Connor unable to follow the pace, with only Quintana and Sergio Higuita (BORA-hansgrohe) able to hold onto the move.
O'Connor set the pace behind in a chasing group, but once into the final few hundred metres, the others in that group attacked, leaving the Australian to battle onto the finish alone.
It came down to a final dash to the line atop the mountain, with Quintana taking up the sprint, but Almeida ultimately having the power to overcome the Colombian just before the finish line and take the win.
O'Connor pumped the pedals to finish 13th on the day, 23 seconds down, which meant that he surrendered the lead of the race.
Quintana assumed the race leadership, with Almeida on the same time and Higuita in third, six seconds in arrears. Previous leader O'Connor slipped down to sixth overall, still well within shot of the overall win at 17 seconds behind Quintana.
The Volta a Catalunya continues with Stage 5, a day that starts in the mountains but looks like it will ultimately be one for the sprinters, with a flat finish in Vilanova i la Geltrú. Watch from 0115 AEDT on SBS VICELAND and SBS On Demand.