Porto crashed to their first defeat of the season as Group G rivals Dynamo Kiev kept their Champions League hopes alive with goals from Andriy Yarmolenko and Derlis Gonzalez at the Estadio do Dragao.
The Portuguese side needed only to avoid defeat against Dynamo to qualify for the knock-out stages, but Yarmolenko's first-half penalty and Gonzalez's clinical second-half effort forced them to put any celebrations on hold.
A draw or defeat combined with Chelsea's 4-0 win in Tel Aviv in the other Group G match would have left Dynamo staring at their 11th straight group exit, but Serhiy Rebrov's side turned in an impressive display to stun the home side.
Porto made the brighter start, with Danilo's sixth-minute header well held by Dynamo goalkeeper Olexandr Shovkovskiy and Yacine Brahimi rifling a 20-yard shot over the crossbar.
Paraguayan Gonzalez twice narrowly missed with shots from outside the area as Dynamo responded and Sergiy Rybalka's 25-yard goal-bound effort was blocked.
Gonzalez's curling shot from the left edge of the area was brilliantly turned over the crossbar by Porto goalkeeper Iker Casillas and Denys Garmash's header hit a post two minutes later as the home side took control.
Dynamo made a deserved breakthrough in the 35th minute. Porto midfielder Giannelli Imbula fouled Rybalka in the box and Yarmolenko converted the subsequent penalty.
Porto skipper Ruben Neves tested Shovkovskiy from 20 yards, and Casillas was forced into another fine save to tip Yarmolenko's drive over the crossbar just before half-time.
The home side reappeared with more intent early in the second period with Cristian Tello's curling effort just off target.
But Dynamo's Junior Moraes spurned a golden chance when firing wide and Casillas was forced to save with his feet from Garmesh's close-range shot before Gonzalez arrowed home an angled drive to put the Ukrainian side two up in the 64th minute.
Porto were almost gifted a goal back when Dynamo midfielder Rybalka turned the ball against his own post while trying to clear, and home substitute Andre Andre's shot hit the woodwork deep in stoppage time, but there was no way back for the Portuguese.