A thrilling game looked set for a 2-2 draw when Aymen Abdennour's clumsy challenge brought down Luis Suarez in the 92nd minute of the game on Saturday, reports Xinhua news agency.
Messi still had work to do to beat Diego Alves, who had saved 19 out of 41 penalties in Spain. Messi's low spot kick had just enough to go in at the post and give Barca the win.
A scrappy start to the game saw Valencia clearly motivated with Enzo Perez putting in some rugged challenges. One of those left Andres Iniesta in agony just after 11 minutes and the Spain international had to leave the field with what was quickly confirmed as a partially torn knee ligament.
Messi put Barca ahead with a typical left foot finish in the 22nd minute, but Barca were lucky the goal stood as an offside Luis Suarez jumped over the ball and clearly blocked the view of Valencia keeper Diego Alves.
Alves then kept Valencia in the game with three world class saves, first to deny Messi his second of the game and then to frustrate Suarez on two occasions, the second of which was all the better as the ball took a deflection on the way through.
Valencia lost Jose Gaya with a muscle injury but should have equalized when Dani Parejo allowed Ter Stegen to make block.
Valencia coach Cesare Prandelli made a brave change at the break bringing on former Barca striker Munir El Haddadi and he was rewarded in the 53rd minute when the youngster leveled the score with a left foot shot.
Things got even better three minutes later when Rodrigo put Valencia ahead with a close range effort following a sublime pass from Nani.
Seven minutes later Barca were level with Suarez on hand to lash a left foot shot home after Alves had produced another fine save from a Rakitic header.
From then on it was end to end stuff with Nani having a chance to put Valencia ahead before the late drama saw Messi's penalty seal it in the dying minutes of the game.