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.
|CONCACAF Champions League Review: Guadalajara poised for quarters, Red Bulls draw|
|Del Potro stunned by Tiafoe at Delray Beach Open|
|Bouchard lawyer satisfied after jury rules against USTA|
|´Enough is enough, we gotta take action´ - Panthers´ Luongo on gun control|
|Former NBA pair help Brazil stay perfect in World Cup qualifying|
|Keane: Wilshere is the most over-rated player on the planet|
|Monfils wins rain-interrupted clash against top seed Cilic|
|Earnhardt says short-track speed skating is ´NASCAR on ice´|
|My knee flared up – Wawrinka reveals reason for retirement|
|Stoger: Dortmund were lucky to survive Atalanta scare|
|Gattuso hopes to continue as AC Milan boss|
|Europa League Review: Arsenal survive scare, Dortmund battle past Atalanta|
|Noren, Simpson share Honda Classic lead after Tiger shoots 70|
|´Complacent´ Arsenal had no ideas, Wenger admits|
|Wilshere pulls no punches after Arsenal´s Ostersunds defeat|
|Batshuayi accuses Atalanta fans of racism|
|Mercedes not responsible for spoiling F1 spectacle, Wolff claims|
|Veteran kicker Vinatieri signs with Colts for further year|
|More injury woe as Wawrinka retires from Open 13 Marseille|
|Villarreal defender Semedo charged with attempted murder|
|West Indies taking nothing for granted in World Cup qualifying|
|Police officer dies after clashes between Athletic Bilbao and Spartak Moscow fans|
|Atalanta 1 Borussia Dortmund 1 (3-4 agg): Returning Schmelzer strikes late|
|AC Milan 1 Ludogorets 0 (4-0 agg): Borini strike secures victory at San Siro|
|Arsenal 1 Ostersunds 2 (4-2 agg): Gunners through despite Emirates shock|
|Kerber back in the top 10 where she belongs, Schuttler proclaims|
|Simeone open to Carrasco, Torres exits amid CSL reports|
|Mancini: Zenit not Europa League contenders|
|Confident Kerber enjoying ´fresh start´ after downing Pliskova|
|Honda Classic grind ´like playing a British Open´, says Woods|
|Europa League Review: Napoli´s comeback in vain, Immobile sends Lazio through|
|Ferdinand has doubts over Kane´s Tottenham future|
|Zenit 3 Celtic 0 (3-1 agg): Ivanovic inspires second-leg turnaround|
|Atletico Madrid 1 Copenhagen 0 (5-1 agg): Gameiro ensures Simeone´s men cruise through|
|Six Nations 2018: Owen Farrell ´jumping out of his skin´ to face Scotland, Eddie Jones says|
|Why De Gea really is untouchable as the Premier League´s best goalkeeper|
|Woods, Thomas start strong at Honda Classic|
|Farrell ´jumping out of his skin´ to face Scotland, says Jones|
|I wouldn´t sell him - Arbeloa wants Bale to remain at Real Madrid|
|Two Grand Slams and 2,253 points scored - Gatland´s 100 Wales matches in Opta numbers|
|Leeds United delay reveal of new club crest|
|Guti urges Neymar to leave PSG for Real Madrid|
|Bottas: I can beat Hamilton to F1 title|
|Vettel and Ferrari ´fired up´ for 2018 after unveiling SF71H|
|Azpilicueta: Chelsea must be perfect to beat Barcelona|
|Rai´s big finish ensures three-way tie in Qatar|
|Roma star Under tipped to win Ballon d´Or before 2023|
|WATCH: Snedeker splashes around for a soggy six at Honda Classic|
|Hamilton says Vettel rivalry ´could be worse´ in 2018|
|Zola denies he will join Sarri at Chelsea next season|
|Barcelona is my club - Sergi Roberto rejects other interest to sign new deal|
|Lewandowski employs Zahavi as agent as Real Madrid rumours continue|
|If you gave me a chainsaw I´d take it off - Wolff not keen on halo|
|Farrell and Porter among five Ireland changes|
|Pardew confirms Evans remains West Brom captain|
|Muntari joins Seedorf at Deportivo La Coruna|
|F1 champs Mercedes launch new W09|
|Wigan coach Wane ponders Hardaker lifeline|
|Hamilton hopes for pre-season contract resolution|
|Winter Olympics 2018: Joy for Gisin, Hirscher´s woe and record-breaking win for Wu|
|Rooney: Moving to China for the money would have been easy|
|Carrasco left out of Atletico squad amid China move reports|
|Arthur: Move to Barcelona still on track|
|Blatter backs Morocco 2026 World Cup bid|
|Moeen backs Rashid, Hales white-ball decisions|
|Wenger sees Welbeck emerging from torrid struggles|
|Zidane stands by selfless Benzema|
|Sodhi earns New Zealand ODI recall|
|´Important names´ keen on Inter star Icardi|
|Hughes returns in only England change|