The reigning Bundesliga champions looked like they had overcome the scare of a fourth-minute Patrick Herrmann strike when goals from Mario Gomez and Bastian Schweinsteiger, who also missed a penalty, fired them ahead before the break.
But Marco Reus equalised after 56 minutes before substitute Igor De Camargo sent Monchengladbach, without a home win this season, ahead.
It stayed that way until the 84th minute when Lahm profited on some good fortune, Anatoliy Tymoschuk's pass deflecting kindly into his path, to score.
Mainz missed the chance to return to the top of the table after they were beaten 1-0 at mid-table Freiburg.
Papiss Demba Cisse's 64th-minute penalty decided the game to leave Mainz a point behind leaders Borussia Dortmund who travel to Hannover on Sunday.
Theofanis Gekas scored twice as Eintracht Frankfurt moved up to third with a 3-1 win over Wolfsburg.
Greece striker Gekas opened the scoring in the 26th minute with Pirmin Schwegler adding another after 38 minutes.
Gekas then struck again from the penalty spot in the 55th minute for his 12th goal in 10 games in all competitions, before Ashkan Dejagah grabbed a consolation for Wolfsburg 11 minutes later.
Hamburg drew level with fourth-place Hoffenheim on 18 points with a 2-1 win over their rivals.
Mladen Petric struck the winner after 83 minutes after Heiko Westermann (45) had cancelled out Hoffenheim's fifth-minute opener through Sejad Salihovic
Nurnberg joined the group of team on 18 points as they thrashed lowly Cologne 3-1.
Jens Hegeler's 11th-minute opener was quickly cancelled out by Pedro Geromel but goals from Ilkay Gundogan and Julian Schieber ensured a home win.
Match Results and Reports