A day after Cristiano Ronaldo set a new Primera Division record for hat-tricks with 23, Lionel Messi grabbed one of his own to move up to 21 himself and secure local bragging rights for Barcelona with a 5-1 win over city rivals Espanyol.

As well as his hat-trick Messi surpassed 400 club goals as Barcelona came from behind having gone behind to a 13th minute Sergio Garcia goal.

The Argentine, who holds the La Liga and Champions League scoring records, produced a now familiar devastating performance to take his record to 402 goals for Barca in all competitions, including 28 in friendlies.

He produced his tour de force, predictably, just a day after Ronaldo, his rival for title of world's best player, scored a La Liga record 23rd hat-trick against Celta Vigo for Real Madrid.

Messi may have two fewer La Liga hat-tricks than the Portuguese but he has 29 to Ronaldo's 27 in official matches for their Spanish clubs.

Former Barca player Garcia put Espanyol ahead at the Nou Camp but Messi, who had earlier hit the crossbar with a free kick, equalised before half-time, his landmark 400th goal coming with a clinical strike from the edge of the area.

Shortly after the break, Luis Suarez found the Argentine and he again hit the corner of the net from a similar distance.

Espanyol were in disarray and Gerard Pique was allowed a free header from a corner to add to the lead after 53 minutes.

Substitute Pedro latched onto a long ball from Jordi Alba to grab another and, with nine minutes remaining, he set up Messi for a brilliantly executed third.

The win leaves Barca in second place in La Liga, two points behind Real Madrid.

"Until the first goal the game was at a slow tempo which is what we are used to as rivals just pack the defence," Barca coach Luis Enrique told a news conference.

"When we conceded, the doubts came and we couldn't find an end product to our play. The key came with the goal before the break and then after that it was all a lot easier."

Barca had plenty of the ball in the first half but when they lost possession, they were too easily opened up. Sergio Busquets gave the ball away and Pique failed to get close enough to Garcia who fired past Bravo to give Espanyol a surprise lead.

Garcia and Victor Sanchez missed further chances but Barca grew as the first half went on, culminating in Messi's equaliser.