Benfica pipped Paris St-Germain to top spot in Group H on away goals thanks to a late sixth goal at Maccabi Haifa.
PSG thought they had wrapped up the group with a 2-1 victory against Juventus in the other game.
But Joao Mario’s 92nd-minute goal took Benfica level with PSG on points, the clubs’ head-to-head record, goal difference and goals scored.
So away goals in all group games was used to separate them – with Benfica scoring nine to PSG’s six.
Benfica went in at the break at 1-1, with Goncalo Ramos netting before Tjaronn Chery equalised with a penalty.
Petar Musa’s 59th-minute goal sent them on their way, with Alex Grimaldo and Rafa Silva netting before Henrique Araujo put them within touching distance with two minutes to go.
Former West Ham United midfielder Mario hammered a sensational sixth into the bottom corner from 25 yards to seal the remarkable win.
It is the first time in Champions League history that the two top teams in a group have had the same amount of wins, draws, defeats, points, goals scored and goals conceded.
Ramos, who scored Benfica’s opener, said: “We were worried about winning, as always, then we felt that it was possible to reach first place and we went looking for more goals.
“As the game progressed, we saw that it was within our reach. I am very happy to have helped the team with another goal.”
Until then it looked as if Nuno Mendes’ second-half winner for PSG at Juventus would be enough for them to finish top.
Kylian Mbappe gave PSG the lead with a fine solo opener before Leonardo Bonucci equalised for Juventus.
PSG coach Christophe Galtier said: “Firstly, congratulations to Benfica. I have to congratulate my team, my squad too.
“We’ve beaten Juventus twice and we were very happy until the 92nd minute. But if you want to go far in this competition, you have to beat big teams. Let’s wait for the draw and see who we get in the round of 16.”
Juve qualify for the Europa League, with Maccabi out of Europe altogether.