Real Sociedad ended Real Madrid's 21-game unbeaten run by winning 4-3 at the Santiago Bernabéu on Thursday to reach the Copa del Rey semifinals for the first time in six years.
The result halted Madrid's eight-game winning streak and handed the club its first loss in all competitions since October.
It also ended Madrid's hopes of winning the cup competition for the first time since 2014.
Real Sociedad, a semifinalist in 2014, will be looking to win its third Copa del Rey trophy — and first since 1988.
The visitors looked in control for most of the match but Madrid came close to pulling off the comeback after scoring late goals in the second half.
Young Norwegian playmaker Martin Odegaard, playing for Sociedad on a loan from Madrid, didn't celebrate after opening the scoring in the 22nd minute off the rebound of a save by Madrid goalkeeper Alphonse Areola.
Young Swedish striker Alexander Isak added to the lead with goals two minutes apart early in the second half — a volley in the 54th and a shot into the top corner in the 56th.
The 20-year-old Isak had already had a goal disallowed for offside after video review just minutes after the break.
Marcelo pulled Madrid closer with a low shot from inside the area in the 59th, but Mikel Merino increased the visitors' lead again from close range in the 69th.
Rodrygo netted Madrid's second in the 81st, two minutes after Vinícius Júnior had a goal disallowed by video review. The third came through a header by Nacho Ramos three minutes into stoppage time.
Sociedad defender Andoni Gorosabel was sent off just before the final whistle.
The Copa is being played in single-elimination games this season, except in the semifinals.
Granada eliminated defending champion Valencia on Tuesday, while second-division club Mirandés got past Villarreal on Wednesday. Barcelona visited Athletic Bilbao later Thursday.
Mirandés will be the only lower-division team in the semifinals.
More AP soccer: https://apnews.com/Soccer and https://twitter.com/AP_Sports
Tales Azzoni on Twitter: https://twitter.com/tazzoni