After falling behind to Sergio Garcia's early strike, Barcelona then took control of the Catalan derby.
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.
"There's no words to describe Messi. He's given us so much that I genuinely think it's not possible to say everything that he's done for us," said Pique.
"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."
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.
Barcelona did not heed the early warning, and they soon found themselves behind.
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.
After being brought down on the edge of the box, Messi saw the resulting free-kick bounce off the crossbar with goalkeeper Kiko Casilla beaten, and it only seemed a matter of time before the home side equalised.
The equalizer arrived on the stroke of half time with the kind of goal that Barcelona have made their trademark.
Jordi Alba cut inside from the left, feeding Xavi Hernandez, who in turn laid the ball off to the Argentine who swept home from just outside the box.
Five minutes after the break it was 2-1 when Neymar combined with Luis Suarez to find Messi on the edge of the box, and the 27-year-old rifled a right-footed effort past a hapless Casilla to knock the wind out of their opponents' sails.
Two minutes later the advantage was doubled as a Rakitic corner was met by Pique, and from there on there was only one winner.
With 13 minutes remaining an Alba diagonal ball found substitute Pedro Rodriguez, who had earlier replaced Suarez, and the Spain international made no mistake to further stretch Barca's lead.
Nine minutes from time Messi capped off a fine afternoon as he scored his third, and his side's fifth, following a give-and-go with Pedro.
Former Manchester United manager David Moyes suffered his first defeat as Real Sociedad boss in a 4-0 thrashing away at Villarreal.
There was a touch of fortune over the first goal just after the hour mark as Bruno's free-kick crashed off the bar and rebounded into the net via the back of Sociedad goalkeeper Enaut Zubikarai.
Real Madrid loanee Dennis Cheryshev then sealed the points with a fine low strike that also clipped the woodwork on its way past Zubikarai before Moi Gomez struck twice in the final 10 minutes to round off the scoring.
Defeat leaves Sociedad in 14th, three points above the relegation zone, while Villarreal move up to sixth.
Valencia's recent struggles continued as their winless streak in La Liga stretched to four games after a thrilling finish in a 1-1 draw at Granada.
Alvaro Negredo looked to have given the visitors all three points when he smashed home his first goal for the club seven minutes from time.
But after Valencia were reduced to 10 men when Paco Alcacer was shown a red card for a clash with Juan Carlos, Isaac Success bundled the ball home two minutes from the end to hand Joaquin Caparros's men a vital point.
Valencia remain in fifth but are now four points adrift of Sevilla in fourth, with the Andalusian side running out 1-0 victors at Rayo Vallecano thanks to Carlos Bacca's 11th goal of the season.