Messi extended Barca's lead before half-time to move on to 25 La Liga goals this season but the Catalans slowed down in the second half and Turkey forward Enes Unal made for an interesting finish by converting a penalty in the 69th minute following a VAR review for a foul by Ronald Araujo.
The Uruguayan defender made amends by heading in Messi's corner in the 87th minute to extend Barca's lead and Antoine Griezmann slammed in a penalty in added time after being handed the ball by Messi.
The win took Barca back up to third place on 68 points, five behind leaders Atletico Madrid but with a game in hand.
"It was a very strange game but the team knew how to get the job done and win which is the important thing," said Araujo.
"We suffered a little bit in the second half, we could have been more comfortable. Every game we have left is like a final and every team will give everything."
Getafe gave Ronald Koeman's side a guard of honour for their recent Copa del Rey triumph but the visitors also wore T-shirts saying "Earn It", criticising Barcelona's decision to sign up to the breakaway European Super League.
"The news had a big impact on me, any clubs want to compete in Europe and we want to earn it on the pitch," said Getafe's Angel Rodriguez.
"We're a humble club who played in the Europa League so obviously I'm totally against it."
After the match, Koeman said: "It's true difficult moments [in the] second half. I think good result in the end. Very good first half, team very focused, a lot of chances, played well.
"[Our] rhythm on the ball was slower, it complicates things with the 3-2 and it's unnecessary. But another show that when we dip, things can become concentrated."