Messi broke the deadlock before lowly Getafe equalized through Clement Lenglet’s own goal.
A mix-up between Sofian Chakla and keeper David Soria restored Barca’s lead and Messi made it 3-1.
Enes Unal’s penalty gave Getafe hope but goals by Ronald Araujo and Antoine Griezmann sealed the points.
Barcelona are five points behind leaders Atletico Madrid, who they host on 8 May, but have one game in hand.
Earlier in the day, Barca issued a statement about signing up to the European Super League, stating it was a “historic opportunity” to guarantee football’s financial sustainability.
On the pitch, Ronald Koeman’s side turned on the style in an incident-packed first half which saw Messi hit the bar and the post for the 2021 Copa del Rey winners.
However, they struggled after half-time and lived dangerously until late goals by substitute Araujo and Griezmann, from the penalty spot, secured the win.