After falling behind to Sergio Garcia's early strike, Barcelona then took control of the Catalan derby yesterday.
Messi's 21st Liga treble, which took him to within two of Cristiano Ronaldo's record of 23 set on Saturday, a Gerard Pique header and neat finish from Pedro, was enough to see Barca take the spoils and leapfrog Atletico Madrid in second place with 34 points.
Real still lead the table with 36 points after a 3-0 win over Celta Vigo on Saturday.
"It was a difficult game. We were much improved in the second half. We fixed our mistakes and what we now have to do is continue like this."
Also Read
Despite having not won at the Camp Nou since 2009 it was the visitors who started the brighter, creating the game's first chance after eight minutes, however Jordi Alba was able to clear Garcia's cross with Lucas Vazquez poised to tap in from close range.
Felipe Caicedo challenged Sergio Busquets just inside the Barcelona half, and as the Camp Nou waited for a whistle that never came from referee Ignacio Iglesias Villanueva, Sergio Garcia ran directly at a static Pique before firing past Claudio Bravo to stun the hosts.
It was Espanyol's first goal at the home of their illustrious neighbours since Ivan De La Pena's double saw them record a 2-1 win there five years ago.
Barcelona then reacted, with Xavi Hernandez, Neymar and Ivan Rakitic all seeing chances come and go as they began to dominate play.