Stevan Jovetic, the former Manchester City player, ended Real Madrid’s 40-game unbeaten record in all competitions when he scored in added time to earn second-placed Sevilla a 2-1 victory over the La Liga leaders. They had come from behind with two late goals at the Ramón Sánchez Pizjuán.

Real had looked on course for their 13th league win of the season after Cristiano Ronaldo had scored a joint-record 56th La Liga penalty in the 67th-minute but with five minutes left the home side were level when the former Sevilla defender Sergio Ramos scored an own goal.

That set the stage for a thrilling finish and Jovetic scored his second goal against Real in four days, having marked his Sevilla debut with a goal after joining on loan from Internazionale in the 3-3 draw in the Copa del Rey on Thursday, although their opponents progressed to the quarter-finals 6-3 on aggregate.

As a result of their win, Sevilla cut Real’s lead at the top of the table to a point, although Zinedine Zidane’s side have a game in hand.

Afterwards the Real defender Marcelo said Real had been a touch complacent. “It’s a tough pill to swallow, dropping points in this way,” he said. “After they equalised they continued to attack and scored their second goal. There’s still a long way to go in La Liga, and we’re still up there. I believe we relaxed a bit today, but we have to learn from it and make sure it doesn’t happen again.”

Sevilla’s manager, Jorge Sampaoli, said “After the [Real] penalty the team continued playing with conviction and courage. The players won a very important game for the club. We were able to beat an undefeated team. I feel a lot of emotion, every game is important here at the Ramón Sánchez Pizjuán.”

Real had exerted early pressure with Ronaldo, who had been rested for the cup tie, hammering a free-kick into the wall and Ramos heading wide from a corner.

Sevilla’s Steven N’Zonzi was a threat in midfield and his harassing of Raphaël Varane allowed Samir Nasri the chance to line up a shot from 20 yards but he fired over.

Ronaldo wasted his best first-half chance after 39 minutes when he failed to connect properly with Karim Benzema’s low cross and the ball rolled wide.

Sevilla began the second half aggressively and Franco Vázquez forced Keylor Navas into a smart save with a side-footed effort in the opening minute. Navas was again tested down low to his right by a rasping Wissam Ben Yedder drive after 54 minutes.

A scramble in the Sevilla box just after the hour mark presented Benzema with a glorious chance but the France striker fired over from eight yards. Soon afterwards, though, Real were ahead. Dani Carvajal, who capitalised on a mistake by Sergio Escudero, was adjudged to have been brought down in the box by Sevilla’s keeper Sergio Rico and Ronaldo scored his 12th league goal of the season from the spot.

Sevilla rallied and to the joy of their fans it was Ramos, booed throughout after his antics in the cup tie when he had celebrated in front of the home support after scoring a Panenka-style penalty, who provided their equaliser when he headed Pablo Sarabia’s free-kick into his own net.

Then, with time almost up, Jovetic snatched the winner with a curling right-foot short from outside the area, which Navas got a hand to but failed to keep out.