Ipswich twice looked to be cruising to victory at 2-0 and 4-1 in front, but in the end they had to survive the final 11 minutes, plus five minutes of added on time before securing what may prove to be a crucial win.
Rotherham started in lively fashion, with Paul McLaren twice firing firm drives at goal, one over the top and the other producing a good save from a diving Kelvin Davis.
Town, who were denied early on by a fine Mike Pollitt save from Darren Bent's firm drive, went ahead on six minutes when Shefki Kuqi and Bent freed Darren Currie on the left and his cross was perfect for Ian Westlake to head home.
Bent toe-poked just wide after racing on to a Kuqi chip over the top and Currie fired in a shot which shaved the bar after Westlake's initial effort had been charged down.
Westlake shot wide from a David Unsworth pass before Town made it 2-0 on 25 minutes, Kuqi's pass being headed forward by Tommy Miller for Bent to lash a volley over Pollitt from 18 yards.
Just as Town looked to be cruising, Rotherham got a goal back straight from the kick-off, Jason De Vos misjudging Rob Scott's long ball, and Tony Thorpe fired low beyond Davis.
Kuqi clipped the top of the bar with a header from a De Vos flick and then Richard Naylor blazed over after Bent teed him up as Town looked to re-establish their lead.
After a quiet start to the second half, Town scored twice in three minutes to apparently kill the game, as first Bent tapped in Westlake's low cross on 57 minutes, and on the hour Jim Magilton fired a 25-yard drive low past Pollitt's right hand.
Miller fired just over before a fierce shot from lively Rotherham winger Jamal Campbell-Ryce was spilled by Davis, and Martin Butler followed up to force the ball home on 67 minutes.
Campbell-Ryce, who had tormented Town all night, was surprisingly subbed two minutes later, with even the home fans giving him a standing ovation to acknowledge his fine display.
Magilton and Kuqi, twice, missed chances to seal the win, and Rotherham continued to pose a threat, with Davis forced to dive and save Butler's shot on a breakaway.
The visitors gave themselves fresh hope on 79 minutes when skipper Martin McIntosh's free-kick from 20 yards was cruelly deflected beyond Davis and Town were rattled at that point.
Only a sensational last-ditch challenge by De Vos denied Butler as he was about to level the scores late on, before Bent missed a stoppage time chance of a hat-trick, failing to control a Magilton through ball.
Town held on and with Wigan beaten at home by leaders Sunderland, they go back into the top two automatic slots, but will need to cut out any similar defensive lapses if they are to seal a return to the Premiership.