Gabriel Paulista headed Valencia into a 50th-minute lead but Barca replied with three goals in 12 minutes to remain within two points of leaders Atletico Madrid, who they host at Camp Nou on Saturday.
Paulista's goal set the tone for a thrilling second half, with Messi equalising from close range after seeing his penalty saved by former teammate Jasper Cillessen.
Antoine Griezmann then gave Barca the lead and a Messi free kick appeared to wrap things up with 20 minutes to play. However, a stunning strike from Carlos Soler set up a nervy final seven minutes for Ronald Koeman's side.
Koeman said before Sunday's game that if his side win their final five games, they will win the league. That's now four. If they do win those four, they're guaranteed to finish above Atletico, although they will still need Real Madrid to drop points. If both Barca and Madrid win all their remaining games, Madrid will win the league courtesy of having the better head-to-head record between the two sides.
This was a gutsy performance from Barca, though, which welcomes encouragement for the Atletico game. They fought back from a goal down to win in the league at Mestalla for the first time since the 2016-17 season and showed fight to close the game out. The comeback was led by Messi, who looks determined to drag Barca to the title.
There was also good news on the booking front. Messi, Griezmann and Frenkie de Jong were among the players who would have missed the Atletico game if they were cautioned. They weren't.
Barca showed frailties in defence again, conceding a soft goal from a corner and being opened up too easily on the counter-attack at times. They were also wasteful in the final third, which meant they were once again unable to kill the game off.
Manager rating out of 10
5 -- Koeman was serving the first of a two-game touchline ban after being sent off during the midweek defeat to Granada. Once the game started, he could have little impact on it, with his No.2 Alfred Schreuder prowling the technical area in his place.
Player ratings (1-10; 10 = best. Players introduced after 70 minutes get no rating)
GK Marc-Andre ter Stegen, 6 -- Made good saves from Uros Racic and Goncalo Guedes while the game was goalless. Left the goal gaping for Valencia's first goal, although felt he'd been obstructed by Thierry Correia.
DF Ronald Araujo, 6 -- Replaced Oscar Mingueza as the right-sided defender. Is better at the back but not as comfortable on the ball.
DF Gerard Pique, 6 -- Battled through a knee injury to win several key headers late on as Valencia sought an equaliser.
DF Clement Lenglet, 5 -- Has improved in recent weeks but switched off to leave Paulista free for the Valencia goal.
MF Sergino Dest, 6 -- Returned to the side after resting in midweek but was unable to offer a threat on the right.
MF Frenkie de Jong, 7 -- Covered every blade of grass in another non-stop performance. Helped in defence and got into the box when he could, with his header, saved by Cillessen, setting up Griezmann's goal.
MF Sergio Busquets, 7 -- Continued his good form from recent weeks and was quickest to react to Messi's penalty miss to keep the ball alive.
MF Pedri, 7 -- Was heavily involved in the game in a positive way from the start but missed a golden chance to open the scoring inside 10 minutes. His blocked shot led to Messi's first goal.
MF Jordi Alba, 6 -- Better in attack than defence and delivered a lot of dangerous crosses into the box, including the one that led to Griezmann's goal.
FW Lionel Messi, 8 -- Involved in most of Barca's best moments. His great pass for De Jong was handled by Toni Lato to win the penalty, which eventually led to his first goal. Added his second, his 28th in the league this season, via a superb free kick.
FW Antoine Griezmann, 7 -- Didn't get in the box enough in the first half but was well placed to score the vital second goal from six yards out to extend his scoring streak to five in his last five games.
MF Sergi Roberto, N/R -- Replaced Dest for the final 15 minutes to help Barca keep the ball.
MF Ilaix Moriba, N/R -- Another sign of the confidence Barca have in him to throw him on in such a big game.