Published: 2017-03-20 04:00:40 BdST
Manchester City loanee Mangala stunned the Nou Camp by heading Valencia in front in the 29th minute but Luis Suarez soon cancelled out the opener following a quick throw-in by Neymar and the Uruguayan then won a penalty as he was hauled down by Mangala, who was sent off for a second booking.
Messi beat penalty-saving expert Diego Alves from the spot for the second time this season to pull Barca level in the 45th minute but Munir El Haddadi, who is on loan from the Catalans, equalised immediately in injury time of the first half.
Barca dominated the second half and took the lead for the second time when Messi smashed the ball past Alves's near post but the visiting side created an opening to level again but Munir squandered the chance.
Former Valencia midfielder Andre Gomes then scored his first goal for Barcelona to make sure of the points.
Barca are second in the standings on 63 points, two behind Real who have a game in hand.
"We controlled the game but they made the most of a set piece and a counter attack. Overall we played well, we played at a high tempo and in general we can be happy with the game," Barca captain Andres Iniesta told reporters.
"The title race is still alive and we're obliged to stay up there and take advantage of any opportunity we get if Real Madrid slip up."
Earlier, Antoine Griezmann scored for the fifth time in four league games and provided an assist as Atletico Madrid beat Sevilla 3-1 to move to within two points of Jorge Sampaoli's third-placed side in the La Liga standings.
The France international delivered a cross from a free kick for Diego Godin to head in the opener in the 37th minute then sent the ball fizzing in off the bar from a set-piece in the 61st while Koke tapped in the third in the 77th to seal a third straight league win for Atletico.
Joaquin Correa scored a consolation goal for Sevilla in the 85th.
While Atletico are in the midst of a good run of results Sevilla are in freefall. They have failed to win any of their last four games in all competitions. They have now lost their last two matches as they went out of the Champions League following a 2-0 defeat at Leicester City on Tuesday.
Sevilla have 57 points while Atletico are in fourth place on 55.
"Sevilla have had a magnificent season, praised by everyone and so this makes our performance even more laudable," Atletico coach Diego Simeone told reporters.
Sevilla boss Sampaoli said his side had made a bad start to the game and lost focus at vital moments.
"We couldn't control the psychology of the game. We were dominated at first then just as we started to control the game they scored from a set piece," he said.
"In the second half the same thing happened and it was all uphill from then on."
Also on Sunday Celta Vigo won the Galician derby at Deportivo La Coruna 1-0 thanks to a late goal from Iago Aspas while Sporting Gijon beat Granada 3-1 to leapfrog the Andalusians into 18th but remain in the relegation zone.
Leganes are five points above Sporting in 17th after drawing 0-0 at home to Malaga.