Bayern Munich thumped Benfica 5-1 to book their place and ease the pressure on coach Niko Kovac.
“It’s a convincing win and that’s exactly what we needed at the moment,” Arjen Robben said after he netted twice.
Roma will also play in the knockout phase despite losing 2-0 to Real while former European champions Ajax won 2-0 in a violence-hit game against AEK Athens to book their last-16 ticket after riot police clashed with Dutch fans.
Marouane Fellaini’s 91st minute winner against Young Boys saw Manchester United squeak through after they had laboured against the Swiss at Old Trafford.
After Fellaini’s low shot went in, United manager Jose Mourinho picked up and threw over a pack of bottles in relief at a result that lifts some of the gloom over the club.
Real and Roma secured their spot in the knockout stages even before the Spaniards’ 2-0 victory in the Italian capital after Group G rivals CSKA Moscow fell to a 2-1 defeat at home to Viktoria Plzen.
Gareth Bale scored one and supplied the cross that led to another as Lucas Vazquez wrapped up a clinical 2-0 win at the Stadio Olimpico that was a world away from Real’s 3-0 Liga defeat to Eibar at the weekend.
“It was important to bounce back quickly,” said Real coach Santiago Solari. “We are very happy.”
Real are three points clear of Roma going into their final Group G match against CSKA Moscow next month.
Mario Mandzukic booked Juventus’ ticket for the knockout phase with a simple tap-in after a sublime piece of skill and cross from Ronaldo as Juventus beat Valencia 1-0.
The Italians have 12 points, two more than United but both are home and dry because Valencia only have five.
In Lyon, City took a point from a pulsating match after David Cornet scored two second-half goals including an outstanding long-range effort only for the French side to be pinned back each time.
City’s French defender Aymeric Laporte equalised first and then Aguero outjumped taller defenders to head home and spark wild celebrations from manager Pep Guardiola.
Lyon are left needing a draw against Shakhtar Donetsk in their final Group F game next month to go through after the Ukrainian club stunned Hoffenheim 3-2 away to stay in contention.
Greek riot police wielding batons entered the stands at the Athens Olympic Stadium to separate fighting fans, leaving 11 Ajax supporters injured, police said.
Some Dutch fans were left with blood streaming down their faces, AFP photographs showed.