Chris Burke was Birmingham's hero as he won the penalty which his fellow Scot, Phil Caddis, scored in the 57th minute to cancel out a 22nd minute strike by Mustapha Carayol.
Middlesbrough appeared to be heading for a long overdue win at St Andrew's when Daniel Ayala scored in the 80th minute.
But it was Burke who again engineered Birmingham's late equaliser with an accurate corner which substitute Bartley headed home four minutes into injury time.
It was Birmingham's second home draw in five days, and despite the late rally must have been another disappointment for manager Lee Clark.
Middlesbrough's new manager, Aitor Karanka, could have mixed feelings. His side battled hard and nearly banished their Birmingham hoodoo having won only once in 16 visits to St Andrew's.
With only six points from their last 21 away games, Middlesbrough were desperate to improve their dismal record and found themselves in a battle with their fellow strugglers.
Birmingham were certainly the more positive in their search for the opening goal with Dariusz Dudka and Jesse Lingard having early strikes. Dudka's effort bounced wide, while Lingard, shooting on the turn, saw his effort effectively blocked by Jonathan Woodgate.
But was Middlesbrough who seized a 22nd-minute lead. Marvin Emnes created the cross from the left which bounced back to Carayol to turn and fire his shot into the corner for the net from eight yards.
After five successive away defeats the goal lifted the visitors who began to play with clear determination against a Birmingham side lacking any rhythm.
Boosted by their success they nearly increased their lead when Albert Adomah narrowly missed a right-wing cross with Darren Randolph floundering in Birmingham's goal.
Surprisingly goalkeeper Shay Given, on loan at Middlesbrough from Aston Villa, was not seriously tested in a first half in which the north-east club appeared to be well in control.
The only shots the Irishman had to save were long-range efforts from Riley and Caddis which the veteran handled with aplomb.
In an effort to inject more life into his attack, Birmingham boss Clark sent on winger Burke in place of on-loan striker Aaron Mclean, and it turned out to be an inspirational move.
There was certainly an improvement, as highlighted when Middlesbrough skipper Woodgate saw his back header from a Mitch Hancox cross hit the post, a whisker away from an own goal.
But it was Burke who was instrumental in Birmingham getting back on level terms as he went down in the area under a challenge from Ben Gibson, and Caddis stepped up to convert the penalty.
Middlesbrough maintained a high tempo and it needed a reflex save from Randolph to keep out a powerful downward header by Kei Kamara.
In the 80th minute Birmingham made several attempts to clear a corner but the ball bounced to Ayala who prodded the ball past Randolph.
Birmingham salvaged their point when Bartley powered home a great header from Burke's corner.