Robert Lewandowski grabbed a dramatic stoppage time winner to send Barcelona back to the top of La Liga.
The Polish striker reached Raphinha’s cross at full stretch to score his 13th goal of the season in the 94th minute.
Lewandowski had been close to scoring before half-time when a header hit the base of the post.
Valencia’s Lino had a goal disallowed for handball after the break, with the result sending Barca ahead of Real Madrid before their match on Sunday.
The result followed a mid-week 3-0 defeat by Bayern Munich that saw Barca knocked out of the Champions League before the end of the group stage.
Their only La Liga defeat came against Real Madrid, and while the battling win in the Mestalla sends them top it is only on goal difference, with Real due to play struggling Girona less than 24 hours later.
Valencia were second best for most of the first half but should have gone into the break ahead, but defender Gabriel sent a free header wide of Barca’s goal.
The home side tired as the second half wore on, with ex-Leeds United midfielder Raphinha chipping in a cross that Lewandowski flung out a foot to meet to give Barca their 10th league win of the season.