Messi winner sees off Malaga
Pep Guardiola's side were under pressure after Real Madrid's in-form striker Gonzalo Higuain had struck twice to lead them to a 5-1 win at Tenerife in the earlier kick-off.
Real's sixth straight victory briefly sent them a point clear at the top on 59 points from 24 matches before Barca replied by seeing off stubborn Malaga.
The Andalucians were pinned back from the start at the Nou Camp where Argentina forward Messi and Swedish striker Zlatan Ibrahimovic having early chances.
Ibrahimovic flashed a long-range free-kick just wide of the post in the 24th minute and Messi shot wastefully over the bar five minutes later.
The visitors defended resolutely, with Weligton in the thick of the action, and the Brazilian cleared an Ibrahimovic header off the line just before the hour.
Pedro, in the starting lineup in place of France forward Thierry Henry, finally broke the deadlock in the 68th minute with a superb long-range strike but Valdo stunned the home side with a breakaway equaliser in the 81st.
Malaga looked to have secured an unlikely point before Spain midfielder Xavi split their defence with a perfectly weighted pass for right-back Daniel Alves and the Brazilian squared for Messi to tap in an 84th-minute winner.
Second-from-bottom Tenerife started brightly at a blustery Heliodoro stadium and Real defender Raul Albiol did well to block a close-range Alejandro Alfaro volley in the first minute.
The home side's resistance lasted until just before the half-hour mark when defender Marcelo played the ball into space and Argentina striker Higuain arrowed a low shot past goalkeeper Sergio Aragoneses.
Central defender Ezequiel Garay made a rare foray forward to set up Real's second in the 42nd, Higuain timing his run perfectly to sweep his compatriot's centre into the net from the edge of the penalty area.
Higuain's double gave him 16 league goals this season, one short of top marksmen Messi and David Villa of Valencia.
Midfielder Ayoze pulled one back for Tenerife a minute after the break before Brazilian playmaker Kaka restored Real's two-goal advantage two minutes later.
Cristiano Ronaldo netted a penalty, his 13th league goal of the season, with 10 minutes remaining and substitute Raul glanced in Real's fifth in added time.
"Things were working for us virtually the whole time," Real coach Manuel Pellegrini told a news conference. "However, you always need to improve and we're still not the perfect team."