The 13-times European champions went behind in the 57th minute when Shakhtar substitute Dentinho capitalised on a lapse in concentration by Raphael Varane to score in an empty Olympic Stadium, where Madrid won their last Champions League in 2018.
Israeli forward Manor Solomon then wrapped up the home side's shock win over the Spaniards in the 82nd with a solo run on the counter and finish into the bottom corner.
Dentinho came on in the first half to replace striker Junior Moraes, who was injured after being hauled down by Varane in a bid to prevent the forward racing towards goal unchallenged.
Shakhtar, who beat Madrid 3-2 away in October, are second in Group B on seven points, level with Zinedine Zidane's side but above them due to their superior head-to-head record.
Real host leaders Borussia Moenchengladbach next week in their final game when Shakhtar visit bottom side Inter Milan.
Madrid's run of qualifying from the group stage for a record 23 consecutive seasons is in serious danger but Zidane vowed to fight on, dismissing suggestions he could resign as he did in May 2018 after lifting a third straight Champions League crown.
"I'm optimistic. This is a difficult moment but we need to show our character now and stand tall," he said.
"What happened today is a real shame because we did not deserve it but we have to win our next game. We're going to believe in ourselves and we're going to fight, you can be sure about that," he added at the post-match news conference.
The Spanish champions pulled off a 2-0 win at Inter in their last European game while the Ukrainians were hammered 4-0 away to Gladbach, though Zidane's side were brought back to earth with Saturday's shock 2-1 home loss to Alaves in La Liga.
They looked the better side before the interval and nearly scored an early goal when Marco Asensio hit the post from a tight angle, but the second half belonged to Shakhtar who savoured another upset win over the kings of European football.
Shakhtar's Solomon said: "It's been a great match and great result for us. Six points in two games against Real is a dream come true. Now we have a good chance to qualify for the knockout stage but we have a very tough game with Inter ahead of us."