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.
|England recall Jennings to replace struggling Stoneman|
|Messi: Barcelona is going to be my only place|
|Messi: I´d give up Barca title for World Cup glory|
|LeBron reaching eight straight Finals is ridiculous and unbelievable – Stevens|
|Indians top Astros in 14 innings as Cole-Bauer duel turns into marathon|
|I´m squeezing this orange until there´s no juice left – LeBron|
|LeBron James leads Cavs past Celtics and into NBA Finals|
|Kyle Busch dominates to win Coca-Cola 600|
|Henderson still confident of Liverpool success|
|Sporting Kansas City 0 Columbus Crew 0: 10-man visitors battle to MLS draw|
|Inglis to captain new-look Maroons, Boyd and Scott axed for Origin I|
|Neymar labels Real Madrid links ´nonsense´|
|Koepka ready to turn seconds into wins|
|Rose ´really proud´ of Fort Worth Invitational win|
|Paul a ´game-time decision´ for series decider, says Rockets coach D´Antoni|
|Egypt president wishes Salah speedy recovery|
|Bonucci: Balotelli has matured|
|Ranieri backs Sarri to succeed at Chelsea|
|Konta admits to doubts after another early Roland Garros exit|
|Warriors´ Iguodala, Looney listed as questionable for game seven|
|Zverev batters Berankis, Goffin back from the brink in Paris|
|Rose cruises to victory at Colonial|
|Neymar: I´m not at 100 per cent yet|
|Toulouse confirm Ligue 1 survival with win over Ajaccio|
|I know what I´ve achieved - Bale not worried about plaudits as he looks to the future|
|Ostapenko ´angry´ after Roland Garros exit, Venus Williams falls at first hurdle|
|Barcelona B relegated to Spain´s third tier|
|Rayo Vallecano win promotion to LaLiga|
|Tarkowski leaves England camp for hernia surgery|
|Ramos ´quite clever´ but Salah injury not to blame for final defeat, insists Robertson|
|WATCH: Ricciardo takes wide receiver role on yacht for Tom Brady pass|
|Until next year – Ronaldo drops new future hint as Real Madrid celebrate with fans|
|Froome stunned by Giro triumph|
|The Triple Crown: Froome´s Grand Tour glory in numbers|
|Hannover sign Wimmer on season-long loan|
|Inter powerless to stop Icardi exit, admits Spalletti|
|Ashton makes England statement with Barbarians hat-trick|
|Watson masterclass seals third IPL title for Super Kings|
|Kozlova ends Ostapenko´s Roland Garros reign on day one|
|Imperious Zverev breezes into second round|
|Froome wins Giro d´Italia to complete Triple Crown|
|Stricken Salah confident over World Cup hopes|
|Verstappen highlights change in approach amid Rosberg criticism|
|Strinic claims three-year deal at Milan|
|Napoli captain Hamsik considering China offer|
|Chinese outsider Wang shocks Venus Williams in Paris|
|Redemption two years in the making - Ricciardo revels in Monaco glory|
|Flawless Molinari holds off McIlroy at Wentworth|
|Karius ´infinitely sorry´ but thanks Liverpool fans for support|
|Root and Bayliss dismiss ´outrageous´ fixing allegations|
|Giggs: Bale the best Welsh player I´ve seen|
|Ricciardo ends Monaco hoodoo despite mechanical issue|
|Lyon star Fekir denies being close to Liverpool switch|
|Pogba: I learned a lot from Abou Diaby|
|Where next for Real Madrid stars Ronaldo and Bale?|
|Shingler and Hewitt out of Wales tour|
|Kyrgios pulls out of French Open|
|Khan wants to bow out by winning blockbuster Brook fight|
|Is winter coming? Game of Thrones stars descend on Monaco|
|Root urges England´s batsmen to ´find a way´|
|Lovren, Van Dijk absolve Karius of blame|
|We all know where our nipples are - Hansen welcomes tackle trial|
|Rooney would thrive in MLS, says DC United boss|
|Pakistan complete demolition of sorry England|
|Curry, Durant reveal the secret to Thompson´s success|
|Salah will miss World Cup, claims Saudi sports administrator|
|Deschamps marvels at Zidane´s incredible Champions League success|
|FIA satisfied Ferrari ´inside the rules´|
|NSW Origin hopeful Moylan pulls the strings for rampant Sharks|
|Champions League blunders will stay with Karius - Clemence|