Eliaquim Mangala headed the visitors in front from a corner, before Luis Suarez steered in an equaliser.
Messi's penalty made it 2-1 after Manchester City loanee Mangala was sent off for dragging back Suarez, but Munir El Haddadi swept Valencia level.
Messi scored his second after the break and Andre Gomes made it four late on.
Barcelona desperately needed a victory after losing ground in the title race with a surprise 2-1 defeat at Deportivo last weekend.
In the end, they should have won by more goals than they did - but they were all over the place defensively for the first hour.
Luis Enrique's three-man back line was caught out too often, with Munir denied by keeper Marc-Andre ter Stegen when clean through seconds before Mangala scored from the resulting corner.
Valencia were equally sloppy in defence, though. Ezequiel Garay was outpaced down their right by Suarez on more than one occasion, and the Uruguay international was quick to pounce for the equaliser when the visitors switched off at a Neymar throw-in.
Mangala, already on a yellow card, could have no complaints about his dismissal just before half-time.
Enrique, though, had plenty of reason to fume when his own defenders allowed Munir, on loan from Barcelona, all the time he wanted to turn in Jose Luis Gaya's pull-back.
Barcelona did not seem to know how to cover their right flank defensively, and Enrique eventually sent on Sergi Roberto in a move to a back four.
That settled the home side, who could have had plenty more goals - with Neymar going closest as he hit the post with a free-kick.
In the end, they settled for two in the second half; Messi - La Liga's leading scorer - caught out keeper Diego Alves with a near-post drive and substitute Gomes tapped in from Neymar's pass. - BBC -