In what was Pep Guardiola's last home match in charge at the Nou Camp, Messi made sure there was a happy ending to what was an emotional night for the home fans.
Thousands had turned out with flags and banners paying tribute to Guardiola and what he has achieved for the club during his four-year spell in charge.
Barcelona may have conceded their 2011 league title to arch-rivals Real Madrid last week, but three-time FIFA Ballon d'Or winner Messi gave the Nou Camp a boost by pulling ahead of Cristiano Ronaldo in the race for the league's top scorer title.
Ronaldo scored a goal in Real's last-gasp 2-1 win over Granada, leaving the Portugal striker on 45 league goals, five behind Messi with one league match remaining.
Messi is now also the undisputed all-time top-scorer in the history of European football, having overtaken the previous record of 67 goals scored by German legend Gerd Mueller for Bayern Munich in a single season in the 1970s.
In the Spanish capital a party atmosphere reigned following Real's 32nd league title triumph but they were heading for defeat until Ronaldo equalised an early Franco Jara goal from the spot ten minutes from time.
Then, in a dramatic finale an own-goal from David Cortes gave Jose Mourinho's men the victory.
It was an unfamiliar looking Real side as Mourinho gave a run out to players who have not been regulars, with Ricardo Carvalho coming into defence and Nuri Sahin and Esteban Granero paired together in the centre of the pitch.
Granada, still looking to secure their own top flight status, took full advantage of their uncertainty as they took the lead after just four minutes through Jara.
The forward stole the ball off Marcelo midway through the Real half and ran through on goal before slotting the ball through the legs of reserve keeper Antonio Adan, standing in for Iker Casillas.
Minutes later Odion Ighalo's strike was blocked well by the keeper and then his follow-up was cleared off the line by Marcelo.
Ronaldo then headed over and Kaka, left unmarked, fired over inside the area but they remained exposed at the back. Marcelo was again called on to head away a Jara shot which was heading for the bottom corner.
Karim Benzema was inches away from equalising shortly before the hour mark with a strike from a tight angle which came back off the post and then Ronaldo shot at the keeper Julio Cesar instead of passing to Gonzalo Higuain.
Ronaldo finally levelled for the champions from the spot after a foul in the area, and Granada suffered heartbreak in the dying moments when Cortes scored an own goal.
Barcelona were without the injured Gerard Pique and Alexis Sanchez while Xavi Hernandez was only fit enough for the bench. Seydou Keita kept his place in midfield and Pedro Rodriguez's return to form in front of goal saw him start in attack.
Barca took the lead after just 12 minutes through a Messi free-kick which clipped off the top of the wall and past keeper Cristian Alvarez.
Messi scored his second from the penalty spot after 63 minutes following a handball in the area by Cristian Gomez, and then completed his hat-trick with a clinical finish with 16 minutes to go.
There was still time for Messi to hit another again from the penalty spot after a foul on Sergio Busquets.
Valencia took control in the battle for third place with a 1-0 win over Villarreal while Malaga crashed 2-1 away to Atletico Madrid.
What the stars mean:
★ Poor ★ ★ Promising ★★★ Good ★★★★ Very good ★★★★★ Exceptional