LEIPZIG, Germany (AP)Leipzig boosted its chances of advancing to the knockout stage of the Champions League by handing titleholder Real Madrid its first loss of the season on Tuesday.

The German club won 3-2 and now only needs to draw its last group match at Shakhtar Donetsk – which drew 1-1 at Celtic on Tuesday – to advance to the last 16.

Leipzig reached nine points in Group F, one behind Madrid, which had already secured its spot in the round of 16. Leipzig is three points ahead of Shakhtar, while Celtic is last with two points.

Madrid had been unbeaten in 16 matches in all competitions this season, with 14 wins and two draws. The European champion played without several regular starters, including Luka Modric, Federico Valverde and Ballon d’Or winner Karim Benzema.

Madrid can still secure first place in the group with a home win against winless Celtic in the last round.

”It’s a loss, but it doesn’t hurt us too much,” Madrid coach Carlo Ancelotti said. ”We will have another opportunity to finish first at the (Santiago) Bernabeu. We have to move on.”

Leipzig got off to a strong start and Josko Gvardiol scored his first Champions League goal in the 13th minute off the rebound of a save by Madrid goalkeeper Thibaut Courtois. Christopher Nkunku added to the lead five minutes later with a shot from inside the area but Vinicius Junior pulled Madrid closer with a header from near the penalty spot in the 44th.

”The first two goals made things hard for us,” Madrid defender Lucas Vazquez said. ”They came out strong and had a lot of intensity in the first half-hour. We improved and had our chances, but after the third goal in a breakaway it became difficult to come back.”

Madrid had a few scoring chances in the second half but substitute Timo Werner sealed the victory for the hosts from close range after a cross by Mohamed Simakan in the 81st.

Rodrygo gave Madrid its second goal by converting a penalty kick late in stoppage time.

”We lacked intensity in the beginning and that can be costly against a team like Leipzig,” Madrid goalkeeper Thibaut Courtois said.

Leipzig reached the Champions League semifinals in 2020 after defeating Madrid rival Atletico Madrid in the last eight. It was eventually eliminated by Paris Saint-Germain.

Madrid had already reached the next round after winning its first four three matches before a draw at Shakhtar in the previous round. Madrid could have secured first place in the group with a draw. It will close out the group stage at home against winless Celtic.

Ancelotti set up the team without Eduardo Camavinga in midfield and Marco Asensio in attack. Eden Hazard came on as a second-half substitute.

Madrid had won its last four away games in the group stage of the Champions League, all without conceding any goals.

Madrid was unbeaten in its last 13 games against German teams, a run dating to the quarterfinals of the 2013-14 season, when it was defeated by Borussia Dortmund but still advanced.

A moment of silence was held before the match in honor of Dietrich Mateschitz, the co-founder of energy drink company Red Bull, who died at the weekend. Red Bull bought fifth-tier club SSV Markranstadt in 2009, rebranded it as RasenBallsport Leipzig, and financed its steady progress through the league system till it was promoted to the Bundesliga in 2016.

AP World Cup coverage: https://apnews.com/hub/world-cup and https://twitter.com/AP-Sports