The champions underwhelmed again after Tuesday’s 4-0 Champions League defeat at Paris St-Germain and had keeper Marc-Andre ter Stegen to thank for a string of great saves.
Messi gave Barca an early lead, before Unai Lopez fired in the equaliser.
The Barcelona forward then slammed in the late winner from the spot.
Victory sees the Catalan giants move two points above Sevilla into second place in La Liga. They have 51 points, one fewer than leaders Real Madrid who have two games in hand.