Ousmane Dembele gave Barca the lead with a powerful effort and Antoine Griezmann had a penalty saved.
Antonio Sanabria smashed home an equaliser just before half-time.
But Messi came on at the break and had a hand in Griezmann’s equaliser and scored twice himself before teenager Pedri hit a late fifth.
The Barca captain dummied Alba’s pass, taking two defenders and goalkeeper Claudio Bravo out of the equation, to leave Griezmann with a tap-in.
He then put Barca ahead from the penalty spot after Aissa Mandi was sent off for handling Dembele’s shot on the line – a decision made after a lengthy video assistant referee review.
See also: Real Madrid Players, Hazard And Casmeriro Test Positive For COVID-19
Messi played a one-two with Sergi Roberto, who backheeled the ball into his path and he smashed it into the roof of the net.
That was his first goal of the season for club or country which was not a penalty. The previous six had all been from the spot.
He thought he had a hat-trick afterwards but his goal was disallowed for a Roberto offside.
It was a much-needed win for Ronald Koeman, whose side had only picked up two points from the previous four league games.
They move up to eighth, one point behind Real Betis.