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.
|Federer outlasts recovering Wawrinka|
|WATCH: Djokovic wins match, loses car|
|Bolt arrives in Sydney for A-League trial|
|Red-hot Bertens beats Svitolina, Halep marches on|
|Snedeker preserves Wyndham lead|
|Halep hits back to torment Tsurenko|
|Raonic fails to break Djokovic stranglehold|
|Emery seeks more ´demanding´ Ozil|
|CAF Champions League Review: Wydad, Al Ahly & TP Mazembe qualify|
|Stephen Curry wants to finish his career with Warriors|
|Hazard among Europe´s best but can improve, says Sarri|
|Tigers boost top-two hopes, Giants close in on fourth|
|Birmingham City 0 Swansea City 0: Monk´s men held despite dominance|
|Inter taking legal action against Tebas over Modric transfer comments|
|Serie A transfer round-up: Zaza joins Torino, Parma sign Gervinho|
|Roma chief Pallotta makes light of De Laurentiis claim|
|Federer starts double-header by seeing off resilient Mayer|
|Ospina joins Napoli on loan from Arsenal|
|Klavan completes Cagliari switch|
|Djokovic holds out against Dimitrov as Raonic awaits|
|New dates confirmed for postponed Serie A fixtures|
|Halep breezes past Barty to set up Tsurenko clash on same day|
|Olesen unable to capitalise on strong start as Jamieson, Waring go clear|
|Hoffenheim tie Kramaric to four-year deal|
|Mina to miss Southampton clash while Gomes is ´weeks´ away|
|Barcelona coach Valverde dismisses Pogba speculation|
|Valverde not prioritising Champions League over defending Barcelona´s LaLiga title|
|Allegri thanks outgoing Juventus icon Marchisio|
|Pants display from Beef? Johnston forced into change of trousers in Sweden|
|Tour champion Thomas has Welsh velodrome named in his honour|
|Gloucester surprised and disappointed by RFU´s swift Cipriani charge|
|Storm skipper Smith´s back injury not ´too serious´|
|Spurs fixture switch doesn´t appeal to Guardiola|
|Brazil call up Andreas Pereira & Arthur but omit Man City stars|
|Mbappe returns to PSG squad, Kehrer set for debut|
|No Messi in Argentina squad as Simeone wins first call-up|
|England man Roy embarrassed by self-inflicted facial injury|
|Atletico Madrid fined for breaching third-party regulations|
|Cipriani charged by RFU after common assault conviction|
|No contact, no negotiation - Leonardo denies AC Milan deal for Milinkovic-Savic|
|Kovacic set for Chelsea debut against Arsenal|
|Fury´s father restrained as Wilder crashes Pianeta weigh-in|
|Kohli and Bumrah fit for win-or-bust Test|
|Stokes ready and desperate to help England, says Root|
|Salah, Mane and Firmino not in competition says Klopp|
|Darmian staying at Manchester United, says Mourinho|
|Guardiola cites fatigue as factor for stricken De Bruyne|
|Kovac approves Rudy departure from bloated Bayern midfield|
|Klopp insists no player leaves without his approval amid Klavan rumours|
|Real Madrid complain to FIFA over Inter´s alleged Modric approach|
|Mourinho ´has never been so happy´ with unsettled Pogba|
|Manly implode amid Barrett uncertainty, Storm´s Smith injured|
|Sarri certain Hazard is staying at Chelsea|
|Liverpool, Champions League, Manchester derby - The matches De Bruyne will miss due to injury|
|Campbell joins Frosinone from Arsenal on permanent deal|
|Schalke sign Lille´s Mendyl to end left-back hunt|
|Stokes back in England team as Curran drops out|
|Ronaldo doesn´t guarantee Juventus the title, warns Allegri|
|Tuchel keen to guide PSG star Mbappe after World Cup heroics|
|Yuri: I can see Neymar joining Real Madrid|
|Milan pounce for Castillejo as Bacca joins Villarreal permanently|
|De Bruyne to miss three months for Manchester City|
|Manchester City talent Arzani completes Celtic loan|
|I know how precious my career is, says regretful Cipriani|
|Paulo Bento named new South Korea coach|
|Milan complete Laxalt deal|
|Poyet suspended by Bordeaux after outburst against board|
|Tuchel will let Neymar and Cavani settle penalties themselves|
|Hooper: We´re ready, bring on the All Blacks!|
|Outstanding Australia deserve our respect - Read|