Marco Asensio struck a last-gasp winner as Real Madrid edged to a 1-0 win at Leganes in the first leg of their Copa del Rey quarter-final on Thursday.
Zinedine Zidane's men, 19 points behind Barcelona in LaLiga, had won one of their last five games in all competitions, amid reports the Frenchman could leave the club at the end of the season, and they looked set to be held at Butarque.
But after Leganes had shone on their first appearance at this stage of the competition, Asensio came up with a cute near-post finish from Theo Hernandez's cross to put Madrid in firm control of the tie.
Madrid were held at home by lower-tier clubs Numancia and Fuenlabrada in previous rounds after first-leg away wins and they struggled to respond to Saturday's 1-0 defeat at the hands of Villarreal, with Mateo Kovacic missing a golden first-half chance.
Jose Naranjo and Claudio Beauvue forced smart stops from Kiko Casilla and Marcos Llorente headed against his own crossbar, adding to the sense of injustice at Leganes' defeat.
Madrid were poor for large parts of the contest, but Asensio's dramatic impact may serve to ease the growing pressure on Zidane ahead of Wednesday's return leg at the Santiago Bernabeu.
GOLAZOOOOOOOOOO! What a goal!!! @TheoHernandez produced an excellent cross from the left and @marcoasensio10 expertly volleyed home, at the near-post, from iust outside the six-yard box!@CDLeganes 0-1 #RealMadrid#RMCopa | #HalaMadrid— Real Madrid C.F. (@realmadriden) January 18, 2018
Madrid were forced into an early change, introducing birthday boy Nacho at the back for the injured Jesus Vallejo, but Zidane's men made a flat start to the game and had to wait until the 23rd minute for a sight of goal, which Raphael Varane headed over the crossbar.
Javier Eraso rifled a long-range drive off-target as Leganes, on a four-game winning streak at home, found a foothold in midfield midway through the first half.
But Madrid should have taken the lead when Kovacic picked Ruben Perez's pocket 25 yards from goal, only for the Croatian to slide his shot tamely wide of the near post.
Gerard Gumbau got a vital touch to deflect Dani Carvajal's drive wide before Lucas Vazquez just missed Asensio's cross on the stroke of half-time.
Casilla had to react sharply to block a close-range Naranjo effort after 56 minutes, Diego Rico nutmegging Carvajal with a superb backheel to send the winger clear.
And Leganes came even closer to breaking the deadlock when Llorente could only direct a free-kick against the woodwork, with Casilla stranded.
Zidane brought on Luka Modric and Isco for more midfield control but Leganes substitute Nordin Amrabat had a more immediate impact, hooking a volley wide moments after his introduction.
With the game becoming stretched, Modric played Vazquez through but Nereo Champagne made a fine block, denying the Spain winger his fourth goal in the competition.
Casilla sharply palmed away a Beauvue volley destined to fly inside his left-hand post with 15 minutes to go and when Kovacic again finished meekly, Leganes looked to have held on.
It was not to be.
Clumsy for the most part, Theo had endured a poor game at left-back but his whipped delivery was perfect for Asensio to get in front of his man to flick home a vital goal for Zidane's Madrid.
|Hummels wary of ´top European team´ as Bayern prepare for Besiktas|
|In-form Kvitova withdraws from Dubai|
|WATCH: ´Crazy´ Evra drives to West Ham training in London cab|
|Barca clash a chance for Chelsea ´to do things right´, proclaims Rudiger|
|Lucas claims Tottenham can win Champions League|
|Federer the ´complete player´ with number-one return - Schuettler|
|LeBron relishes one more link-up with Irving|
|Barcelona tie will not decide Conte´s Chelsea future, says Deco|
|Cordner signs bumper deal with Roosters|
|Lewandowski won´t be distracted by Real Madrid rumours|
|Bennett signs on until 2019 with England|
|Man Utd must not underestimate Sevilla - Navas|
|Coleman demolishes 20-year-old 60m world record|
|Red Bull reveal ´special edition´ livery on 2018 car|
|Iniesta will not be made welcome at Stamford Bridge, warns Azpilicueta|
|Ballack sees ´a big difference´ between Bundesliga and Premier League|
|Karmichael Hunt faces no conviction, but will remain sidelined|
|Betis boss would have preferred to face Madrid´s BBC|
|Chelsea v Barcelona: Messi´s blues against the Blues|
|James leaving Madrid was the best thing, says father|
|He´s looking good – Zidane thrilled with Ronaldo|
|It needs to take 30 seconds – Matic wants quicker VAR|
|Team LeBron edges Team Stephen in thrilling All-Star Game|
|McIlroy sees progress despite failing to contend at Riviera|
|Silver supports NBA players speaking out on social issues|
|Mickelson ready for Masters after third straight top-six finish|
|Gattuso: My job is to be the players´ nightmare until season´s end|
|I wasn´t good enough – Guardiola reveals Wigan rejection|
|Dillon reigns supreme with overtime win at Daytona 500|
|Bubba Watson ends drought with third Genesis Open title|
|Brazilian derby between Vitoria and Bahia abandoned after red-card frenzy|
|Anderson ends barren spell with New York Open triumph|
|It was crazy, but I like that - Zidane revels in eight-goal Betis thriller|
|Asensio credits Zidane for Madrid´s thrilling Betis comeback|
|Zidane hopeful over Marcelo hamstring injury|
|Overton replaces injured Plunkett for Blacks Caps ODI series|
|Bonaventura claims Milan can catch Inter|
|Bravo for Claudio! Guardiola to pick keeper at Wembley|
|Mourinho hoping for reinforcements at Sevilla|
|´Quick-healer´ Groves hopeful over shoulder injury|
|Real Madrid become first side to score 6,000 goals in LaLiga|
|Real Betis 3 Real Madrid 5: Asensio at the double in LaLiga classic|
|AC Milan 1 Sampdoria 0: Bonaventura makes it 10 unbeaten for Rossoneri|
|Schurrle excited to finally link up with Reus and Gotze|
|Fit-again Hughes included in England squad|
|New number one Federer revels in ´unbelievable´ week|
|Real Madrid defender Marcelo suffers hamstring injury|
|Costa fumes referees ´want to give me a card´, but Simeone plays down penalty row|
|Thiem regains Argentina Open title|
|Stoger makes Dortmund history with Gladbach win|
|Championship Review: Leeds fight back for point, Norwich & Ipswich share derby spoils|
|Racing beat La Rochelle to go second, Castres edge out Bordeaux|
|Borussia Monchengladbach 0 Borussia Dortmund 1: Burki unbeatable as Reus returns to haunt Foals|
|FA Cup ´magic´ no surprise to Pochettino after Rochdale thriller|
|Rochdale hero Davies: This is every kid´s dream|
|Atletico Madrid will not stop in pursuit of Barcelona, promises Gameiro|
|Banks boot pays dividends for Treviso|
|Lucky 13 and second successive title for Kvitova after Muguruza classic|
|´Wrong image´ to blame for VAR confusion in Manchester United´s FA Cup win|
|Rochdale 2 Tottenham 2: Davies denies Kane in FA Cup classic|
|Rieder stretchered off as Wasps extend Chiefs´ slump|
|Atletico Madrid 2 Athletic Bilbao 0: Gameiro and Costa settle cagey clash|
|NHL commissioner Bettman condemns ´reprehensible´ Smith-Pelly abuse|
|Visser, Scott and Nel among six recalled by Scotland|
|Bhuvneshwar and Dhawan inspire India´s Wanderers win|
|Napoli set club record with another Serie A win|
|Froome disappoints in time trial as Wellens wins Ruta del Sol|
|Napoli 1 SPAL 0: Allan sends hosts top of Serie A again|
|Federer makes short work of disappointing Dimitrov|
|Allegri allays Higuain injury fears|