Shakhtar Donetsk beat Real Madrid 2-0 in the Champions League on Tuesday, leaving the 13-time European champions hopes of advancing hanging in the balance.
Dentinho made the most of a defensive lapse early in the second half and fellow substitute Manor Solomon secured the result late on as the Ukrainian side completed the double over Madrid in the group.
Shakhtar goes ahead of Madrid courtesy of a better head-to-head record. Both teams are one point behind Group B leader Borussia Monchengladbach, which hosts Inter Milan later.
Madrid hosts Gladbach in the final group match while Shakhtar visits Inter.
Madrid has advanced to the knockout stage of the Champions League in all 24 of its previous group-stage campaigns.
Real Madrid knew it could qualify for the Round of 16 with a win against Shakhtar and it was close to an early breakthrough, but Marco Asensio hit a good pass from Karim Benzema off the left post from close range.
Asensio was also denied by Shakhtar goalkeeper Anatolii Trubin, who did well to push his strike over the bar.
Shakhtar came out in the second half more energized and Madrid goalkeeper Thibaut Courtois, who had little to do in the first half, pulled off an early save to deny Taison.
That proved to be a warning sign as Shakhtar broke the deadlock in the 57th minute when a rapid counterattack followed by a defensive mix-up allowed Dentinho to sneak in and fire into the bottom left corner.
Another counterattack saw Shakhtar double its lead eight minutes from time as Solomon raced down the left flank before cutting in and drilling into the near bottom corner.