A double from Lukas Jutkiewicz inspired Birmingham to a 2-1 win and Championship safety to leave Derby deep in relegation trouble.
Derby had taken a first-half lead through Colin Kazim-Richards but Birmingham hit back with Jutkiewicz twice heading past Kelle Roos.
The victory means Birmingham cannot be overtaken but Derby are still in danger after a fifth successive defeat.
Although they are four points clear of the drop zone, Rotherham still have two games in hand and Derby have to get something from their remaining two matches.
One win in 12 games had left Derby looking over their shoulder and the early signs were not encouraging as both sides struggled to keep the ball and create openings.
Derby had a couple of free-kicks just outside the area but the delivery was poor and Birmingham also wasted promising positions with a loose final pass.
The game badly needed a goal and it came in the 36th minute, when Lee Buchanan got behind Birmingham on the left and his low cross was tapped in from close range by Kazim-Richards.
It was the first moment of quality in a game littered by cheap fouls and misplaced passes but as Derby went in at half-time they were edging towards safety.
The second half started positively for County, with Kazim-Richards firing narrowly over from 20 yards and Tom Lawrence making a strong run into the box before a defender stepped across to clear.
Birmingham had not threatened Derby's goal but that changed in the 57th minute when a cross broke to Kristian Pedersen, who saw a goalbound shot blocked by Matt Clarke.
The visitors were getting balls into the box and they levelled in the 62nd minute when a long throw was flicked on and Jutkiewicz powered a header into the top right corner.
Birmingham should have had another in the 74th minute when a low cross from the right flashed across the six-yard box but Alen Halilovic smashed a shot against a post from a tight angle.
It was a big let-off for Derby but they failed to take advantage as Birmingham scored again in the 84th minute.
A cross was allowed to reach Jutkiewicz at the back post and the striker stooped to head into the bottom corner to leave Derby in trouble.
Martyn Waghorn had a chance to salvage a point in stoppage time but his 20-yard free-kick went just wide.
What the managers said...
Derby boss Wayne Rooney: "I thought for the majority of the game we coped really well but two key moments have cost us the game.
"I questioned the players' fight and character and attitude on Tuesday. I can't do that today, I thought they gave everything and left everything on the pitch, but the result is disappointing."
Birmingham head coach Lee Bowyer: "With two games to go, it's some achievement. We had 10 (to play) when I came in and we've only lost one against a good Watford side, so to turn it around and say we are safe with two games to go is very good.
"This is why the players need some credit because when I came through the door, mentally they must have been in a really bad place. To be able to turn it round in a short space of time is tough. I've been a player and know if you're in a bad place you might need a little bit of luck or something, but as a group they've just done the basics and then they started to grow and grow."