Real Madrid's Dani Ceballos scored an 89th-minute winner to secure a 3-2 victory at Union Berlin in their final Champions League Group C match on Tuesday, to make it six wins from six matches as the Germans ended their European run in last place.
The hosts had started well and Real goalkeeper Kepa Arrizabalaga needed to pull off a spectacular one-handed save after just 55 seconds to deny Kevin Behrens from close range.
The Spaniards responded with a glancing header from Joselu that bounced off the crossbar in the 16th while he also fired wide in the 43rd before Luka Modric had a 45th-minute penalty saved by Frederik Ronnow.
Instead it was hosts who scored in a dramatic finale to the first half, with Kevin Volland pouncing on a David Alaba miskick to slot home in stoppage time.
Union needed to win to have any chance of qualifying for the Europa League but they came under increased pressure.
Union's Ronnow came to their rescue once more in the 55th, palming a Rodrygo header from point-blank range wide but the Dane was beaten by Joselu's header at the far post six minutes later.
The Real forward struck again in the 73rd with another header, this time at the near post from a superb Fran Garcia run and cross but the Germans briefly levelled through Alex Kral's low drive in the 85th.
Ceballos made sure of the win in the 89th to lift Real to 18 points with Napoli also advancing to the knockout stage, finishing in second place on 10. Braga finished third on four with Union in last place on two points.