The hosts had looked much the better side in the first half, while City played without conviction. But a dreadful mistake by young left-back James Husband led Rovers to a self-inflicted defeat - their first home loss in nine league games.
Defender Paul Quinn fired Doncaster in front in the 36th minute and City goalkeeper Darren Randolph produced a couple of brilliant saves to prevent them going further behind.
But there was a remarkable transformation after the interval when Husband's error allowed Macheda to equalise in the 57th minute. A low shot from Lee Novak was helped into the net by Paul Quinn in the 71st minute before Macheda scored again with a fine header five minutes later to wrap up the points.
Doncaster suffered a couple of bad injury blows when captain Ritchie Wellens and key defender Abdoulaye Meite were both ruled out at the last minute.
But they looked the more lively side in a scrappy first half against a Birmingham side lacking in confidence.
Precious few chances were created in the early stages, but the best one fell to Rovers when Chris Brown latched onto a long ball from James Husband in the seventh minute.
Brown got his head to the ball but mistimed his effort and the header sailed harmlessly wide.
Macheda had a tame header collected by Sam Johnstone at the other end after Novak had floated over a left-wing cross a couple of minutes later.
City were relying too much on the long ball and the better football came from the home side.
Chris Burke hit a cracking 25-yard shot just wide in the 14th minute before James Coppinger replied for Doncaster with an effort that went narrowly wide.
The game badly needed a goal and it came in the 36th minute following a free-kick from David Cotterill.
Brown headed Cotterill's cross across the face of goal and defender Quinn fired home a fine left-footed shot at the second attempt after his first effort had rebounded off a defender.
Birmingham looked dispirited but Darren Randolph produced a decent save to prevent Brown from scoring in the 41st minute before Novak despached a shot on the turn just wide.
Randolph produced another outstanding reflex save to keep City in the game five minutes into the second half. Cotterill floated over a free-kick from the left, and Luke Neill's point-blank header was instinctively palmed away by the goalkeeper.
But things started to unravel for Doncaster in the 57th minute when Husband dwelt on the ball in the penalty area and allowed Macheda to rob him of possession and fire home from an angle.
A long kick from Randolph led to Novak rounding Johnstone to fire towards goal in the 71st minute and Quinn could only help the ball into the net in his attempt to clear as the visitors went ahead.
Macheda completed the turnaround when he headed home five minutes later after Tom Thorpe had headed a free-kick from Paul Robinson back across goal.
Doncaster's attempts to fight back were feeble, although the outstanding Randolph still had to make a couple of good saves from Theo Robinson and Billy Sharp in stoppage time.