MADRID (AP) — With a remarkable injury time goal, Real Madrid salvaged a 3-3 draw against Sevilla in the Copa del Rey to set a Spanish record of 40 consecutive games unbeaten on Thursday.

Madrid had its spot in the Copa quarterfinals all-but-secured in the final minutes in Seville when Karim Benzema made a great run past several defenders and entered the area to fire in a low right-foot shot that allowed Madrid to surpass the unbeaten mark that it shared with rival Barcelona.

The dramatic equalizer started with consecutive back-heel passes between Benzema and left back Marcelo outside the area. Benzema's shot deflected off a defender's leg before crossing the line.

Madrid advanced 6-3 on aggregate following its 3-0 first-leg win at home last week.

"(Sevilla) played a great match and deserved more tonight," Madrid coach Zinedine Zidane said. "But if you look at both games, we deserved to advance."

Madrid kept alive an unbeaten streak that started nine months ago. Madrid has won 30 games and drawn 10 in a run that includes three titles.

"We are happy to have achieved our goal against a very difficult rival," defender Sergio Ramos said. "We didn't win but we kept our streak alive."

Cristiano Ronaldo and other regular starters were left out of the starting lineup to rest on Thursday.

Benzema scored the winner less than 20 minute after coming off the bench at Ramon Sanchez-Pizjuan Stadium.

The hosts got on the board with an own goal by Madrid defender Danilo just 10 minutes in. But Marco Asensio dampened the crowd's hopes with a remarkable 70-meter run that culminated with an equalizer from inside the area just after halftime. He got the ball near his own area and made it all the way to the other side before finding the net.

At that point, Sevilla needed four goals to advance, and it added two through Stevan Jovetic and Vicente Iborra. But Madrid netted again near the end with a penalty kick converted "Panenka" style by Ramos, who was returning from injury. Ramos celebrated by provoking part of the crowd that had been insulting him during the game.

"I apologized to the fans who didn't disrespect me like those who have been tainting the image of Sevilla supporters," said Ramos, a native of Seville and a former Sevilla player. "In the end, this will always be home for me."

It was a matchup between the top two teams in the Spanish league, and a rematch of the Super Cup final that Madrid won in extra time five months ago.

The teams meet again in Seville in a league match on Sunday, when Madrid will defend its four-point lead. Zidane's team also has a game in hand.

Madrid is trying to win its first Copa since 2014. Last season, it was disqualified in the opening round for fielding an ineligible player.

Sevilla reached the final last season but lost to Barcelona, which advanced to the quarterfinals on Wednesday by beating Athletic Bilbao 3-1 to reverse a 2-1 first-leg loss in Bilbao.

On Tuesday, Atletico Madrid lost at home 3-2 to Las Palmas, advancing 4-3 on aggregate after a 2-0 win in the Canary Islands.

CELTA VIGO 2, VALENCIA 1

Valencia remained winless this year after failing to reverse a 4-1 home loss to Celta Vigo, being eliminated 6-2 on aggregate. Valencia, just outside the relegation zone in the Spanish league, hasn't won since the resignation of former Italy coach Cesare Prandelli during the winter break.

EIBAR 0, OSASUNA 0

Eibar comfortably held its 3-0 first-leg win, advancing after a lackluster home draw against the last-place team in the Spanish league.