- Karim Benzema (14', 61')
- Fernando (39')
The striker opened the scoring and then put Madrid back ahead in the second half after Fernando equalised for the visitors.
- Don't have ESPN? Get instant access
Benzema's opener was a landmark strike for the LaLiga giants as they became the first club to score 1,000 European goals.
The France international limped off before the end of the game but said it was down to fatigue rather than an injury.
"It was a difficult game for us. Shakhtar are a good team, a very good opponent," Benzema said. "We suffered together and we won in the end. Sometimes you win games like that.
"We weren't at our best like in the first game, but we won. We play every three days and it's a bit difficult. It's a lot of games at a high level. Like today, we didn't play well but we won. The most important thing is the three points. You can't always play well."
Benzema scored after 14 minutes after a Shakhtar defensive mix up saw Vinicius Junior set him up with an easy tap in.
The visitors made it 1-1 just before the break when Fernando finished off a swift move.
Madrid were booed off at half time and withheld pressure early in the second half as Benzema grabbed his second after 61 minutes.
The hosts needed Thibaut Courtois to produce a fine save in stoppage time to come away with the three points.
"We started well, we were aggressive in both halves, we scored the first goal, we needed to be more aggressive and a bit more effective with the ball," Madrid boss Carlo Ancelotti said.
"We tried to have control without doing damage. That's what I said. The two changes, [Dani] Carvajal was to avoid physical problems, I brought Nacho on to control the right wing, and Karim was a bit tired, I brought on [Luka] Jovic, I didn't want to change the shape in attack or defence."