The Argentine scored in the fourth and 90th minutes, the latter via a penalty, to earn the Catalonians all three points against a Leganes side that were minutes away from a most unexpected result.
After the midweek shellacking in Paris in the Champions League, Luis Enrique's team needed a dominating performance against the newly-promoted club from Madrid. Instead only more questions will be asked of this Barcelona side, which failed to impress and were fortunate that Leganes' finishing was lacking.
Manager Luis Enrique made several changes from the team that lost 4-0 to PSG, with Jeremy Mathieu coming in for Gerard Pique at center-back and Lucas Digne getting the nod over Jordi Alba at left-back. Andre Gomes was the lone holdover in midfield, as Sergio Busquets and Andres Iniesta were rested for Ivan Rakitic and Rafinha, respectively.
The best friend Barcelona could have had on Sunday was an early goal and the MSN obliged inside the first five minutes. Neymar played an ounce Luis Suarez down the left flank, and Uruguayan's cross met the right boot of Messi who had the easiest of pushes into the net to give Barcelona a 1-0 lead.
But Leganes showed some steel when they broke to counterattack and Nabil El Zhar should have attracted the visitors from the capital level when in one-on-one with Barcelona goalkeeper Marc-Andre ter Stegen, only for the German stopper to snuff out two attempts from the Leganes striker.
There was still a quiet, nervous feel to those inside the Camp Nou as the match progressed the half-hour mark. A nice one-two combination between Messi and Suarez produced a good chance that was saved by Leganes goalkeeper Herrerin, but otherwise it was slim pickings.
Leganes were clogging the middle of the field with eight or sometimes nine men, forcing Barcelona out wide. And when a chance arose on the counter, Leganes used their speed to unsettle Barcelona center-backs Mathieu and Samuel Umtiti.
The uneasy first-half for Barcelona was capped up by an El Zhar shot that nearly deflected in right before half-time. Ter Stegen made the save, but it did little to dissuade the Camp Nou faithful to send their team to the dressing room with a smattering of whistles.
The chances kept coming for Leganes into the second half and again Ter Stegen was called into action, this time denying Guerrero with a kick save.
As time ticked over into the final 30 minutes, victory for Barcelona was far from a sure thing. A second goal would have greatly eased the tension that had settled into the Camp Nou, but Barcelona's lack of precision and Leganes' confidence going forward dictated otherwise.
Things started to open up and moments after Guerrero spoiled a great chance for Leganes to draw level, Rafinha had the second Barcelona goal on his boot, but pulled the trigger late and eventually saw his chance saved by Herrerin.
The breakthrough that Leganes had come so close to getting would finally arrive 20 minutes from time, thanks in large part to a massive defensive error from Sergi Roberto. The Barcelona right-back was stripped by Guerrero, who fed substitute Machis and the Venezuelan cut back for another substitute, Unai Lopez, who fired in past Ter Stegen to make it 1-1.
Luis Enrique made a trio of changes for the final 10 minutes in hopes of finding the winner, with Denis Suarez, Iniesta and Alba all coming on. The introduction of Iniesta was notable for the sole reason that Andre Gomes was fiercely whistled by the Camp Nou while departing the pitch.
The stage was set for a dramatic finish and whether Barcelona could conjure up a late winner or if Leganes could earn the unexpected draw. Neymar would prove to be decisive, as the Brazilian drove into the area, he was upended by Mantovani and after a second of hesitation, referee Ignacio Iglesias Villanueva pointed to the spot. Messi stepped up and just barely converted past Herrerin to give Barcelona the 2-1 lead.
There was hardly a feeling of euphoria among the Barcelona players after the goal. If anything, it was more relief tinged with disgust. Messi barely even moved after scoring while Pique on the bench didn't even break a smile.
To their credit, Leganes was not about to give up, and in stoppage time the hard-luck El Zhar fired just wide of the post, capping what was a very uncomfortable but ultimately victorious outing for Barcelona.
2.- FC Barcelona: Ter Stegen; Sergi Roberto, Umtiti, Mathieu, Digne (Jordi Alba, min. 82); Andre Gomes (Iniesta, min. 80), Rakitic, Rafinha (Denis Suarez, min. 80); Messi, Suarez, Neymar
1.- Leganes: Herrerin; Tito, Mantovani, Siovas (Insua, min. 87), Diego Rico; Erik Moran (Unai Lopez, min. 65), Alberto, Gabriel; El Zhar, Guerrero, Szymanowski (Machis, min. 70)
Goals: 1-0, min. 4: Messi; 1-1, min. 70: Unai Lopez; 2-1, min. 90: Messi
referee: Ignacio Iglesias Villanueva (Galicia).
Yellow Cards: Luis Suarez (Min. 47) for Barcelona, Matonvani (Min. 56) for Leganes, Siovas (Min. 87) for Leganes, Alberto Martinez (Min. 95) for Leganes
Red cards: none
Stadium: Camp Nou
Luis Enrique: I understand the jeers towards me after the PSG defeat
It has been a week to forget for those within Barcelona and after a late 2-1 victory over Leganes on Sunday, Coach Luis Enrique has made clear he understands the fans' whistling towards him after the 4-0 defeat to Paris Saint-Germain.
The atmosphere at the Camp Nou on Sunday was a difficult one with sections of supporters clearly unhappy with the Asturian's management of the team as well as the uncertainty surrounding the future of his position.
"The situation is what it is, I understand the criticism towards me after the difficult game in Paris," he told the assembled press.
Leganes presented Los Cules with a difficult challenge as the hosts simply could not find any rhythm to their play for much of the evening and Enrique felt this was due to a lack of confidence.
"We knew it would be complicated, we controlled the match but after the Champions League we lacked confidence at times even after our goal," said the Asturian.
It is an old saying in football that coaches are judged by their signings and it still rings true today with summer arrival Andre Gomes on the receiving end of some unfortunate criticism from his own supporters.
Whilst he could understand the whistles aimed at himself, Luis Enrique was unable to understand the targeting of the Portuguese midfielder.
"I don't understand it, I don't understand whistling a player of your own team, it doesn't make sense but of course everyone has a right to their opinion," the coach added.
When asked if or not the atmosphere had played a part in the poor performance the Blaugrana boss was unsurprisingly non-committal but he did make clear the players on the pitch need full focus.
"The players are on the pitch and they have to be focused on the game completely," he concluded.
Ter Stegen: I don't understand the Barcelona fans whistling
A potentially disastrous night for Barcelona was rescued by an injury time Lionel Messi penalty but with tension rising in stands of the Camp Nou, Marc-Andre ter Stegen made clear his confusion at the displeasure of supporters.
It was a night when the pressure on Los Cules to win and win well was monumental following the midweek collapse against Paris Saint-Germain in the Champions League.
Anger towards Luis Enrique was vented rather vociferously at times throughout the match and it made for a rather unpleasant atmosphere with other sets of fans making clear their disagreement with the aforementioned sentiments.
Ter Stegen made clear post-match that the whistling from the fans left him bemused and that the team and fans need to move forward united.
"I don't understand it, the most important thing is that we have won after a very difficult week for everyone and we need the fans, especially at home because we win and lose together," he explained to reporters.
The clash with Leganes itself was by no means a classic but the German stopper made clear that it would provide a pivotal stepping stone towards the upcoming clash with Atletico Madrid.
"There are always things we can improve on but that is normal in football, we have to turn our attention to recovering now because next week we have a very important match," he added.
Barcelona never truly found top gear on Sunday evening and were almost embarrassed by the newly promoted team however the 24-year-old wanted to focus solely on the positivity of the result.
"It was complicated in the end but we are happy because we scored late on and have taken three points, that is what matters," remarked ter Stegen.
Oblak returns for Leverkusen match
Atletico Madrid received a boost on Sunday with the news that starting goalkeeper Jan Oblak will be available for this Tuesday's Champions League trip to play Bayer Leverkusen.
A December shoulder injury required surgery, but the Slovenian has now been given the medical all-clear and will travel with the squad to Germany on Monday.
On the other hand, Diego Godin has not recovered in time from the muscle injury suffered in Los Rojiblancos' Copa del Rey match at the Camp Nou, while Lucas Hernandez will not travel with the rest of the squad as he must be in court on Tuesday morning, after which he'll fly out alone.
The full list of players traveling to Germany is as follows: Miguel Angel Moya, Jan Oblak, Andre Campos Moreira, Sime Vrsaljko, Jose Maria Gimenez, Stefan Savic, Lucas, Filipe Luis, Gabi, Koke, Saul Niguez, Thomas Partey, Nico Gaitan, Caio Henrique, Yannick Carrasco, Angel Correa, Fernando Torres , Kevin Gameiro and Antoine Griezmann.
Ibrahimovic fires Manchester United into the FA Cup quarter-finals
Manchester United's Zlatan Ibrahimovic came off the bench to score the winner as they recovered from an early deficit to beat Championship fightrs Blackburn Rovers 2-1 away in the FA Cup fifth round on Sunday.
The Swedish hitman entered the fray just part of the hour mark at a murky Ewood Park and produced a typically silky finish in the 75th minute to spare United holders an unwanted replay.
It was his first goal in the FA Cup and 24th in all competitions and sent 12-time United winners into the quarter-finals where they will play at Premier League leaders Chelsea.
United's passage was far from smooth, though, against the former Premier League champions who are now struggling to retain their second-tier status.
Until United manager Jose Mourinho sent on Ibrahimovic and world record signing Paul Pogba in the 62nd minute the visitors were finding Blackburn a tough nut to crack.
Danny Graham had put the hosts ahead before United forward Marcus Rashford equalized just before the half-hour mark.
Then, in a moment of supreme quality, Pogba's flighted pass found Ibrahimovic running in behind the Rovers rearguard and the veteran striker took his time before guiding a shot past keeper Jason Steele.
"Did they give us a good game? More than good, they gave us a hard game and congratulations to them," Mourinho, who won the Cup 10 years ago with Chelsea, said.
"Their approach was brave, strong. They had real competitors and if we didn't have the right attitude from everybody we would be in real trouble."
Mourinho even graciously applauded as Rovers took the lead with a goal of real quality in the 17th minute.
The lively Marvin Emnes did superbly to work some space before slipping an astute pass to Graham who finished superbly into the far corner with an angled shot.
"We conceded a brilliant goal," Mourinho said, although it was matched by the quality of United's equalizer.
There looked a little danger when Henrikh Mkhitaryan received the ball inside his own half but he dissected the Rovers defense with a pass played with the outside of his left foot and Rashford rounded Steele before sliding home.
United dominated thereafter but still needed some inspired intervention from their substitutes to seal victory.
Blackburn never gave up the cause, though, and thought they had scrambled a late equalizer when Anthony Stokes poked the ball home but a linesman's flag cut short the celebrations.
United's success means the fixtures are coming thick and fast as they prepare to visit St Etienne in the Europa League last 32 on Wednesday, protecting a 3-0 first leg lead, then face Southampton in the League Cup final at Wembley next Sunday.
"Southampton is sleeping, is eating and resting and working for the League Cup final and we have had to play three tough games," Mourinho said.
"It's so much more difficult for us, but for good reasons," added Mourinho, who can now also look forward to visiting his old club Chelsea in the FA Cup sixth round.