Mesut Ozil saved Madrid with a brilliant late free-kick to earn a Champions League point in an entertaining 2-2 draw with Borussia Dortmund in the Santiago Bernabeu on Tuesday.
Borussia were ahead until the 89th minutes thanks to a Marco Reus strike and an Alvaro Arbeloa own goal in the first period. Pepe had earlier levelled for the home side.
The result means, with two games remaining, the Germans still lead Group D with 8 points, 1 more than Madrid, with Ajax 3 points behind after a 2-2 draw away at Manchester City.
It could have been worse for Madrid as it seemed for long periods the Germans would repeat the 2-1 result from the first game between the sides a fortnight ago in Dortmund.
Afterwards, coach Jose Mourinho was certain his side had been deprived of a goal that was disallowed for offside early in the second-half.
"In the first-half we were not good and our crowd told us so, but we were better in the second. I am not unhappy with the draw, but yes with not winning when we scored 3 goals, because (Jose) Callejon should have had a goal allowed, he was in a legal position and it should have stood," he said.
Borussia's coach Jurgen Klopp was happy with the result.
"We are still top so a draw was a good result for us. If we had won I think we would have deserved it, I'm proud of our performance but a little sad as we were so close to direct qualification," he said.
For Madrid, Luka Modric was chosen to partner Xabi Alonso in the centre of midfield with Raphael Varane again called upon into the centre of defence to allow Sergio Ramos to cover at right-back.
Borussia captain Sebastian Kehl was able to play with a protective mask, despite picking up a facial injury on Saturday against Stuttgart.
The German keeper Roman Weidenfaller had to be quick of his line on 2 minutes to deny Higuain after Angel Di Maria had played a long through pass.
However, backed by around 10000 noisy fans the Germans settled quickly into the game.
First Lewandowski burst down the left on four minutes and fired over a dangerous cross that had to be expertly dealt with by Madrid's defence
On ten minutes Iker Casillas was quick to stop a Marcel Schmelzer effort with his feet and Lewandowski fired over the rebound.
Next it was Kevin Grosskreutz's turn to test Casillas after a quick counter-attack and mazy run down the left from Gotze.
Cristiano Ronaldo should have done better when heading wide a Higuain cross from a good position on 22 minutes, but it was a sign of life from the home team and it lifted their support.
Both sides were now jostling for control of midfield and the opening goal, that eventually fell to the Germans on 28 minutes.
Right-back Lukasz Piszczek was deep in his own half when he played a searching ball that Lewandowski headed into the path of Reus, who powerfully volleyed home first-time.
The Germans seemed momentarily on top and comfortable until Pepe powered home a Mesut Ozil cross with his head on 34 minutes.
Borussia regained the lead right at the death of an exciting half.
Lewandowski's was again prominent, heading a long kick from Weidenfeller into the path of Grosskreutz who burst into the area and laid on a pass for Gotze to bundle the ball past Casillas.
A desperate lunge from Madrid defender Arbeloa provided the decisive touch, but it was a well-worked goal from the Germans.
Mourinho looked for a reaction, bringing on Jose Callejon and Michael Essien to replace Modric and Gonzalo Higuain.
Immediately, Callejon had the ball in the net, but the effort was ruled marginally offside.
Mourinho's last throw of the dice was to introduce Kaka with 13 minutes remaining and he quickly instigated a move that Ronaldo should have finished.
Callejon had a weak shot cleared of the line by Grosskreutz before Ozil's late escape act curling a free-kick over the wall and just inside a post to make it impossible for Weidenfeller and save Madrid at the last.