Shakhtar, who are being forced to play their home matches in Poland due to the ongoing Russian military invasion in Ukraine, looked set to record a remarkable victory against the reigning champions, but Antonio Rudiger's last-gasp header spared the visitors' blushes.
The former Chelsea defender was sent up front late in the match by his manager Carlo Ancelotti in a desperate attempt to salvage something from the match, and it was a direct tactic that ended up working to great effect.
The warning signs were there for the Ukrainian Premier League champions as Rudiger narrowly headed Toni Kroos's cross wide of the post, with the equalising goal almost a carbon copy incident as Kroos's in-swinging ball found his target once again.
Rudiger bravely met his compatriot's cross to head the ball in off the post, before being absolutely clattered by Shakhtar's goalkeeper Anatoliy Trubin, who arrived late to the scene rushing out from his goal.
In scenes reminiscent of Terry Butcher for England during qualifying for the 1990 World Cup, Rudiger left the pitch with his face and kit covered in blood, but it was a blow worth taking as his goal sent Los Blancos into the knockout rounds for the 26th successive season.
Trubin's opposing goalkeeper Andriy Lunin, who has played nine times for Ukraine and was standing in for the injured Thibaut Courtois, had earlier kept Real in the match by denying Lassina Traore with his team 1-0 down.
The Burkina Faso forward looked to have rounded Lunin after being played clean through on goal, but the 23-year-old managed to divert his opponent's effort onto the bar and safely over.
After an uneventful first half, Shakhtar took the lead moments after half-time when Oleksandr Zubkov rose highest to meet Bohdan Mykhaylichenko's cross from the left, with Ferland Mendy's defending at the back post somewhat unconvincing.
Zubkov belatedly saw his fizzing effort from distance tipped wide by Lunin, but his side could not hold onto the lead that he had provided them with, and they have dropped one point behind RB Leipzig in Group F as a result.