Messi was at his brilliant best scoring all of Barca’s goals, and his third of the day, from a classy run and finish past Espanyol stopper Cristian Alvarez in the second half, gave him 50 league goals for the season.
Having reached his hat-trick, Messi gave departing coach Pep Guardiola an emotional embrace on the sidelines as Guardiola leaves the Camp Nou with 13 titles to his name in four seasons.
Messi has had a simply remarkable season, scoring 72 goals in all competitions, becoming the first player to have scored 70 goals in a first division season since Archie Stark did it for Bethlehem Steele in the American Soccer League in 1924-25.
Granada appeared set to boost their chances of avoiding the drop as they took the lead in the fifth minute through Franco Jara and held onto it until 10 minutes from time when Cristiano Ronaldo equalised from the spot after earning a penalty for his 45th goal of the season.
Then showing the kind of fighting spirit that has helped win them the title, Real Madrid kept pressing for a winner and they got it when Cortes inexplicably turned Karim Benzema’s cross into his own net during the third minute of added time.
Third-placed Valencia also left it late as Jonas fired home in the second minute of injury time to earn a 1-0 victory over relegation-threatened local rival Villarreal, who sit just one point above the drop zone.
Malaga, meanwhile, is clinging to fourth on the Spanish table and the last Champions League qualifying position, despite a 2-1 loss to fifth-placed Atletico Madrid.
Malaga took the lead in the 38th minute through Eliseu, but conceded late goals to Koke and Adrian to find themselves just two points ahead of Atletico heading into their last game of the season against Sporting Gijon.