Madrid entered the game having gone three matches without a win, and threatened to lose further ground in the title race when Joselu's early strike put Espanyol ahead.
However, a solo effort from Vinicius Junior swiftly levelled things up, before Militao nodded home at the back post to put the hosts on top before half-time.
Asensio escaped in behind to net a third late on, ensuring Madrid went six points behind Barcelona ahead of the Blaugrana's trip to Athletic Bilbao, with the Clasico rivals due to face off in a huge clash next week.
Espanyol stunned the Bernabeu with their first real attack eight minutes in, Joselu guiding a side-footed finish into the top-right corner after Ruben Sanchez broke beyond Eduardo Camavinga to cross.
Madrid almost went 2-0 down when Vinicius Souza's header forced Thibaut Courtois into action, but it was level after 22 minutes as Vinicius Junior skipped inside to bend a terrific finish into the bottom-right corner.
With Espanyol coming under intolerable pressure, Militao gave Madrid the lead six minutes before the break, heading Aurelien Tchouameni's delicate delivery in off the crossbar.
A last-ditch clearance from Vinicius Souza denied Rodrygo as Madrid continued to press after the interval, before Leandro Cabrera scuffed an effort wide at the other end.
Rodrygo then rattled the crossbar with a fine free-kick as Espanyol faded, before substitute Asensio applied a calm one-on-one finish to make the points safe in stoppage-time.