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."
Sorry, we were unable to load match stats, please try again.