Despite conceding the opening goal from Joselu's thunderous strike in the eighth minute, Real Madrid showed their resolve and levelled the scoreline through Vinicius Jr's individual brilliance in the 22nd minute.
They took the lead before half-time as Eder Militao powered home a header from Aurelien Tchouameni's cross.
Real Madrid continued to dominate in the second half, and Rodrygo's free-kick attempt rattled the crossbar in the 75th minute.
Marco Asensio then sealed the victory in injury time, ensuring that Real Madrid claimed all three points and climbed to second place with 56 points, just six points adrift of Barcelona, who will face Athletic Bilbao on Sunday.
Espanyol were valiant but could not play against Los Blancos, they remain in 13th place in the league standings. Real Madrid, on the other hand, will look to build on this win as they face two crucial fixtures against Liverpool and Barcelona that could shape their season.
The victory comes as a welcome relief for Real Madrid, who had previously struggled to win their last two league games against Atletico Madrid and Real Betis.