Pep Guardiola’s side fell behind early in the second half when Oscar Wendt scored for Borussia Monchengladbach.
Lars Strindl and Fabian Johnson added goals in quick succession for the home side, who moved into third spot.
Ribery, on as a substitute for his first match since injuring an ankle in March, struck with nine minutes left.
Bayern had dropped just two points in 14 league matches this season, and have looked imperious despite uncertainty over the future of coach Guardiola, who is out of contract at the end of the season.
Former Arsenal and Germany goalkeeper Jens Lehmann had suggested during the week that Guardiola’s team could go through the whole Bundesliga season without defeat.
They were on top in the first half, with Borussia goalkeeper Yann Sommer making superb saves to deny Robert Lewandwoski and Thomas Muller, before Kingsley Coman hit the post.
But Sweden left-back Wendt surprised them nine minutes into the second half, slotting in after being set up by midfielder Raffael.
Borussia struck twice more in three second-half minutes, with striker Strindl finishing well from Granit Xhaka’s knockdown, before Fabian Johnson took on Julian Korb’s through ball and placed his shot beyond keeper Manuel Neuer.
France midfielder Ribery, brought on for the final 15 minutes, found the net after being set up by Arturo Vidal, but it was too late.