Rotherham moved up to third in the League Two table as they came from behind to snatch victory at Spotland.
Kari Arnason's header appeared to have put them in front but the referee ruled it had not crossed the line.
Rochdale then went in front when Ashley Grimes fired home his fifth goal in six games from Joe Rafferty's cross.
Kayode Odejayi scrambled an equaliser following a corner and on-loan Courtney Cameron curled home a last-minute winner after the ball ran loose.
VIEW FROM THE DRESSING ROOM
Rochdale manager John Coleman told BBC Radio Manchester:
"We played well in the first half, but we were poor in the second half.
"Rotherham put us under a lot of pressure, they were very direct and their physical approach paid off in the end.
"Although we dealt quite well with them, the pressure's told in the end."
Rotherham United manager Steve Evans told BBC Radio Sheffield:
"I think we were very unfortunate to go behind in the game and we hit the woodwork a couple of times.
"We said to the players at half-time that we're disappointed with the goals they have conceded but we said we believed in them.
"This squad has great character.
"We're not kidding ourselves, this is a tough place to come. It was a thoroughly deserved three points but we did have to work hard."
Rochdale 01 Lillis 02 Rafferty Booked (Bennett - 70' ) 04 Cavanagh 05 R Edwards 12 P Edwards 07 Kennedy 08 Tutte 11 Grimes (Donnelly - 76' ) 14 McIntyre 22 Grant Booked 27 Gornell Substitutes 31 Smith 03 Bennett 06 Barry-Murphy 13 Putterill 23 Craney 09 Donnelly 15 Adebola Rotherham United 21 Warrington 06 Mullins 33 Ridehalgh 04 Arnason 08 Taylor 10 O'Connor 14 Bradley Booked 15 Pringle (Revell - 69' ) 27 Frecklington Booked 16 Odejayi 22 Agard (Cameron - 57' Booked ) Substitutes 30 Thompson 05 Sharps 31 Cameron 32 Walker 34 O'Connell 09 Revell