For long periods on Wednesday, it looked as if Bayern would squander the opportunity given to them by Dortmund's shock defeat by Fortuna Duesseldorf on Tuesday.
Both sides came within inches of taking the lead in the first half. Robert Lewandowski was denied by a fine save from Peter Gulacsi at one end, while Dayot Upamecano thundered a header against the crossbar at the other.
Niklas Suele and Joshua Kimmich both missed chances from close range in the second half. Two red cards for Renato Sanches and Stefan Ilsanker crowned a scrappy game late on, but not before Ribery had finally swept in an 83rd-minute winner to breathe new life into the title race.
Bayern remain in third, but are now five points ahead of fourth-placed Leipzig. Eintracht Frankfurt, meanwhile, remain hot on Leipzig's tail despite being held to a 2-2 draw at local rivals Mainz. Robin Quaison fired Mainz ahead on ten minutes before Luka Jovic pulled Frankfurt level on the half-hour mark.
Quaison restored the lead just five minutes later, turning in a dipping cross from Jean-Paul Boetius. Not to be outdone, Jovic headed in a second equaliser moments before half-time.
Schalke slip deeper into trouble
Earlier on Wednesday, Schalke were plunged further into crisis as they lost 2-1 to Bayer Leverkusen in a cagey clash between the Bundesliga's two underperforming teams.
Defeat leaves Schalke just a point above the relegation play-off place, having won just one of their last six league games.
Leverkusen opened the scoring on 26 minutes when Leon Bailey struck a curling shot against the post and Aleksandar Dragovic prodded in the rebound.
Alario doubled the lead less than ten minutes later, swivelling on the edge of the box to send a low volley underneath Ralf Faehrmann.
Having looked helpless for much of the first half, Schalke were given a glimmer of hope on the stroke of half-time when Haji Wright bundled the ball in at a corner to pull a goal back.
Yet the hosts wasted several chances to draw level in the second half. Steven Skrzybski missed the target from close range, while Yevhen Konoplyanka skewed wide.
Elsewhere, Werder Bremen held Hoffenheim to a 1-1 draw, while a bottom-of-the-table clash between Freiburg and Hanover also ended one goal apiece.
Bremen had several chances early on, including a remarkable miss at point blank range from Yuya Osako. The Japanese came to regret his poor finishing when Leonardo Bittencourt put Hoffenheim ahead at the other end.
The home side, who had been accused of using a drone to spy on Hoffenheim in the build-up to the game, equalised through Theodor Gebre Selassie shortly the break.
Freiburg took the lead with just two minutes played, Luca Waldschmidt converting a penalty after a clear handball from Hanover captain Waldemar Anton.
Ten minutes later, Felipe hooked the ball in from a corner to draw Hanover level and lift them off the bottom of the table.