Atletico Madrid snatched a place in the Champions League last 16 by riding their luck to win 3-1 at Porto on Tuesday in a tense, ill-tempered game as both sides finished with 10 men.
Antoine Griezmann put Atletico in front against the run of play in the 56th minute, bundling the ball into the net following a corner after the hosts had missed a glut of clear chances either side of the break.
Atletico's lead was threatened when Yannick Carrasco was sent off in the 67th minute for raising his hands to Otavio as tempers raged following a foul.
However, Porto's numerical advantage lasted only a few moments as Wendell was dismissed for appearing to lightly strike Matheus Cunha with his elbow, sparking a melee involving the players and coaching staff.
Diego Simeone's team saw out the game in typical fashion, ceding Porto the ball but closing down the spaces, and substitute Angel Correa clinched the win with a clinical finish on the counter-attack in the 90th minute.
Rodrigo de Paul then added a third goal 60 seconds later as Porto ran out of energy and hope.
The home side earned a consolation penalty which Sergio Oliveira converted with the final kick of the game.
Atleti began the game bottom of Group B but ended it second on seven points, with Porto coming third with five and qualifying for the Europa League.
"This match and this group stage campaign sums up just what Atleti is all about," said Griezmann.
"We are a true group and we need everyone to do their bit to get to where we want to. We never stop believing."
Liverpool finished top with 18 points after winning 2-1 at AC Milan, who came bottom with four.
Real Madrid top group
Real Madrid finished top of Group D as goals from Toni Kroos and Marco Asensio secured a 2-0 home win over Inter Milan who had Nicolo Barella sent off.
Both teams had already booked last-16 places and Real ended on 15 points from six games, five more than second-placed Inter. Sheriff Tiraspol finished third on seven and drop into the Europa League while Shakhtar Donetsk came last on two.
Real captain Luka Modric, man of the match in his 100th appearance for the club in Europe's premier competition, was delighted they ended the group stage in pole position.
"Very happy about the game and the result. We wanted to finish top in order to avoid stronger teams (in the round of 16)," the 36-year old Croatia playmaker told reporters.
"I feel good, as if I was under 30. My age is not a factor, what matters is how you perform on the pitch. We played well on the counter and Toni scored a great opening goal."