A late equaliser by the substitute Marco Reus allowed Borussia Dortmund to peg back the holders, Real Madrid, and win Group F after setting a scoring record of 21 goals in the group stage of the Champions League.

Karim Benzema scored in each half at the Santiago Bernabéu Stadium to give Madrid a 2-0 lead, but the forward Pierre-Emerick Aubameyang pulled one back for the German side before Reus struck in the 88th minute. “In the end it was a happy outcome for us,” the Dortmund coach, Thomas Tuchel, said. “It was very important to stand up to Real Madrid twice. We played full of courage.”

Dortmund had drawn 2-2 with Real when they met in Germany. Cristiano Ronaldo’s team were left with the consolation of matching the club’s 34-game unbeaten streak that had stood since the late 1980s.

“We had a lot of opportunities to put the game away and we didn’t,” the Real coach, Zinedine Zidane, said. “We played a good match but couldn’t get the win, now we have to move on. In the end it was well-deserved for them [Dortmund] for what they have done so far.”

Dortmund finished the group with 14 points, two more than Real. Legia Warsaw, who defeated Sporting Lisbon 1-0 in Poland, finished third with four points to qualify for the Europa League.

“To win the Champions League it doesn’t matter if you are first or second in the group,” Benzema said. “You have to beat all the teams.” Ronaldo was kept scoreless for the fourth straight game in the Champions League, again missing the chance to become the first player to score 100 goals in European competitions. His total remains at 98.

Benzema opened the scoring from close range after 28 minutes, connecting with a low cross by Dani Carvajal from the right flank. The Frenchman added to the lead early in the second half with a firm header after a cross by James Rodríguez.

Aubameyang scored from inside the area and then set up Reus’s equaliser after a quick counterattack. Aubameyang’s goal helped Dortmund match the previous record of 20 group-stage goals, a mark that had been achieved by three other clubs: Barcelona, this season and in 2011-12, Real in 2013-14 and Manchester United in 1998-99.

Dortmund’s campaignincluded 6-0 and 8-4 routs against Legia. The draw allowed Zidane’s team to match Madrid’s unbeaten run set under coach Leo Beenhakker in the 1988-89. Real are undefeated since April at Wolfsburg in last season’s quarter-final first leg. “It’s important to continue to make history and continue our good run,” Zidane said.

Ronaldo’s best chance was a close-range shot that struck the far post in the 78th minute. He had also come close 10 minutes earlier with a right-foot strike that forced a cross-handed save from the Dortmund goalkeeper, Roman Weidenfeller.

He remains the competition’s all-time leading scorer with 95 goals, two more than Lionel Messi, who netted his 93rd in Barcelona’s 4-0 win over Borussia Moenchengladbach on Tuesday. It was Messi’s 10th goal in the group stage, one short of Ronaldo’s record of 11 achieved last year.

Ronaldo only scored two goals in this season’s group stage, his worst mark since joining the Spanish club in 2009.