Los Che led early in the second period after initially trailing to a first-half Messi strike, but Luis Suarez levelled the scores just past the hour, before Messi won the match in spectacular fashion late on.
The result has moved champions Barcelona ahead of Sevilla, Real Madrid and Atletico Madrid to the top of La Liga, while Valencia remain in 15th position, just two points outside of the relegation zone.
It was the home side that made most of the early running in the opening 10 minutes of action, with Portuguese duo Nani and Joao Cancelo both causing problems for Barcelona with their movement in the final third of the field.
The first major incident of the match occurred in the 13th minute, however, when Andres Iniesta was forced to leave the field on a stretcher after an awkward challenge from Enzo Perez. Ivan Rakitic was the player introduced for the visitors, who are now facing the possibility of being without their captain for a long period.
Barcelona responded well from the setback, however, and took the lead in the 22nd minute when Messi found the bottom corner with a low strike after a clever pass from Rakitic. There was a hint of controversy about the opener due to Suarez, who was offside, blocking the view of Valencia goalkeeper Diego Alves, but the referee awarded the goal.
The home side had the chance to level less than five minutes later when Cancelo drove down the right before delivering a cross in to Rodrigo, but the forward just could not make contact under pressure from Samuel Umtiti.
The contest really opened up as the first period continued to gather pace and Messi brought a smart save from Alves in the 32nd minute, before Rodrigo had a strong penalty shout turned down after a challenge from Umtiti.
The opportunities continued to arrive and Alves again had to be in inspired form to keep out Suarez 10 minutes before the break, before denying the same player three minutes later as Barcelona looked for a killer second goal before the interval.
As it transpired, it was Valencia that had their best chance of the first period in the 44th minute, but Dani Parejo, after dancing into the Barcelona box, could not beat Marc-Andre ter Stegen as an intense first half finished with Messi's strike separating the two teams.
Valencia boss Prandelli made one change at the interval - introducing on-loan Barcelona forward Munir El Haddadi, who faced the possibility of harming his parent club's title bid in the second 45 minutes.
Barcelona had a chance to score a second in the 50th minute when Alves pushed a low strike from Neymar into the path of Rakitic, but the Croatian could only rattle the post as an end-to-end match continued to entertain.
Less than two minutes after Rakitic hit the post, Valencia were level and the goal came from Munir, who found the bottom corner with a driven effort from distance as the Spain international netted against the club that pays his wages.
Valencia were rampant during the period that followed, and scored their second in the 56th minute when Nani chipped a delicious pass in to Rodrigo and the forward found a route past Ter Stegen with a clever finish as the Mestalla rose to its feet.
Barcelona answered back just past the hour, however, as Suarez smashed the ball past Alves from close range after the Valencia goalkeeper had made a flying save to keep out a header from Rakitic.
It was the visitors that pressed for a third as the match approached its final 10 minutes, but Messi could not make the most of a couple of free kicks, while Suarez was guilty of missing the target on more than one occasion.
Nani had a wonderful chance to send Valencia ahead eight minutes from time after having the freedom of the Barcelona box, but the experienced Portuguese missed the target from close range.
There was to be incredible late drama, however, as Aymen Abdennour brought Suarez to the ground inside the penalty box in second-half stoppage time and Messi stepped up to win the game for new league leaders Barcelona in spectacular fashion.
Next up for Barcelona is a home match against struggling Granada next Saturday afternoon, while Valencia will attempt to return to winning ways when they travel to Deportivo La Coruna on October 31.