(CNN) -- Real Madrid snatched a 3-2 victory at Valencia to remain five points behind Spanish leaders Barcelona, who triumphed 1-0 in the Catalan derby with Espanyol earlier on Saturday night.
Real, missing the suspended Cristiano Ronaldo, needed an 83rd-minute winner from substitute defender Ezequiel Garay as all five goals came in the second half at the Mestalla.
Madrid had twice gone ahead through Garay's fellow Argentinian Gonzalo Higuain but Valencia -- who won last season's corresponding fixture 3-0 -- battled back through Spain striker David Villa and Joaquin Sanchez.
Sergio Ramos headed against the post in the first half, but Real suffered a big blow when fellow defender Pepe was stretchered off just before the interval after the Portugal international fell awkwardly.
Higuain, who turned 22 on Thursday, headed the opening goal from Karim Benzema's cross nine minutes after the break, but Villa leveled six minutes later as he headed in off the underside of the crossbar from Jeremy Mathieu's cross to make up for David Navarro's close-range miss just beforehand.
Marcelo then set up Higuain for his eighth goal this season -- all of which have come in the past seven games -- but substitute Joaquin beat goalkeeper Iker Casillas at his near post in the 80th minute.
However, Garay's glancing header from Xabi Alonso's free-kick kept Real in touch with Barca, who have played one more match.
The Spanish and European champions needed a controversial penalty from Zlatan Ibrahimovic to edge past their city rivals Espanyol at the Nou Camp.
The Swede slotted his 11th league goal this season, one behind top scorer Villa, after Xavi went down in the area despite minimal contact from former Barcelona youth player Raul Baena six minutes before halftime.
The hosts started with Thierry Henry in the absence of injured European player of the year Lionel Messi, but the Frenchman and Ibrahimovic were unable to take advantage of several chances.
Pep Guardiola's team will now head to Dubai for the FIFA World Club Cup, where they will meet Atlante in the semifinals following the Mexican team's 3-0 win against New Zealand champions Auckland on Saturday.
Espanyol suffered a fifth straight defeat, but cannot end the weekend in the relegation zone thanks to Real Zaragoza's 2-1 defeat at home to Athletic Bilbao earlier on Saturday.
The Basque side bounced back from last weekend's 2-1 home defeat to Valencia to go seventh above Sporting Gijon, who host fourth-placed Sevilla on Sunday.
Sevilla are one point behind Valencia, who trail Barcelona by 11 and Real by six.