Paul Caddis's 93rd-minute equaliser saw Birmingham escape relegation to League One in dramatic style.
The Blues needed a draw at Bolton to stay up but looked doomed when they trailed 2-0 to goals by Lee Chung-yong and Lukas Jutkiewicz.
But Nikola Zigic gave Lee Clark's side hope when he pulled a goal back with a header with 12 minutes to go.
And a desperate finish saw Caddis force the ball home in stoppage time to send Doncaster down instead.
Birmingham, relegated from the Premier League in 2011 just months after winning the League Cup, had seemed set to return to the third tier for the first time in 20 years.
They had begun the day in the bottom three and were always relying on results elsewhere to give them a chance of staying up - with their biggest hope being that Doncaster would lose at Leicester, meaning a point would be enough.
Helped by the vocal support of 3,500 travelling fans, they tried to take the game to their hosts but struggled to create chances in the first half.
Birmingham fans endured an anxious afternoon before seeing their side stay up
Lee Novak saw two shots saved in quick succession by Andy Lonergan, but otherwise the Bolton keeper was largely untroubled.
Bolton also created little in a cagey first period but they took the lead after 57 minutes when David Wheater met a free-kick and Lee rifled an angled finish past Darren Randolph.
News that Doncaster had gone behind at Leicester brought hope - and cheers - from the travelling Blues fans but that was followed by despair when Jutkiewicz's shot squeezed past Randolph at his near post.
Birmingham were far from finished, however, and when Zigic headed home a Mitch Hancox cross it meant that, with Doncaster still trailing, Clark's side were again just one goal from safety.
The final few minutes of the 90 were agony for the away fans to watch, as Birmingham poured forward but could not find a way through Bolton's defence.
When six minutes of stoppage time were signalled, however, the Blues still had a chance and Caddis, who joined from Swindon earlier this season, emerged as their hero.
He was in the right place to nod home from close range after Zigic saw his header cleared off the line by Tim Ream, and wheeled away in delight to celebrate with his team-mates.
Blues manager Clark and his backroom staff dashed down the touchline to join them, and the final whistle brought more scenes of celebration - and relief - from the Blues' supporters.
Birmingham City manager Lee Clark and his bench celebrate securing Championship survival