Barca needed to come from behind to keep the pressure on Madrid as Eliaquim Mangala headed Valencia in front.
But the on-loan Manchester City defender was sent-off for pulling down Luis Suarez after the Uruguayan had levelled and Messi smashed home the resulting penalty.
Barcelona plot 'miracle' comeback against PSG
Munir El Haddadi briefly levelled for the 10 men before half-time, but Messi and finally Andre Gomes with his first Barca goal sealed the three points.
Madrid still also have a game in hand over their title rivals.
"We created a huge number of chances and deserved to win by a bigger margin," Barca boss Luis Enrique told Spanish TV station Movistar. "Against an opponent with very good players we've picked up three valuable points."
Messi’s last minute penalty rescues Barcelona
Enrique named the same side that beat Paris Saint-Germain 6-1 10 days ago to progress to the Champions League quarter-finals in stunning fashion.
And whilst not matching that drama, there was no shortage of action as Valencia showed up the defensive flaws in Enrique's new 3-4-3 system.
An action packed final 15 minutes of the first-half saw four goals and a red card.
Barca's weakness from set-pieces was exposed again a week on from conceding twice from corners in a 2-1 defeat at Deportivo la Coruna when Mangala powered home Dani Parejo's corner to open the scoring.
The lead lasted just six minutes as Suarez raced onto Neymar's throw-in to slot into the far corner.
Mangala went from hero to villain as he then pulled down Suarez inside the area to concede a penalty and be sent-off, which was converted by Messi.
However, straight from kick-off Barca were caught cold as Jose Gaya teed up Munir El Haddadi to score against his parent club for the second time this season.
The champions still had 45 minutes to make their man advantage count, though, and they did so despite missing a host of chances after the break.
Messi drilled home at Alves's near post seven minutes into the second period for his 41st goal of the season.
Suarez somehow slotted wide with the goal gaping after Alves had denied Messi his hat-trick, whilst Neymar hit the bar.
But the hosts finally put the game to bed a minute from time when Neymar crossed for Gomes to tap home against his old side.
COMMENTS
Comments are moderated and generally will be posted if they are on-topic and not abusive.
For more information, please see our Comments FAQ