The Bucs (1-8) had built a 15-0 lead in the second quarter but the Dolphins reeled off the next 19 points to grab the lead, capped by a 30-yard Caleb Sturgis field goal with 10 seconds left in the third quarter.
But Mike Glennon led Tampa Bay on a 9-play, 80-yard drive early in the fourth, which ended on Rainey's go-ahead TD.
Rainey finished with 45 rushing yards on eight carries while Glennon completed 11-of-21 passes for 139 yards with a touchdown and an interception.
Brian Leonard picked up 57 yards on 20 carries for the Bucs.
Ryan Tannehill made good on 27-of-42 passes for 229 yards with two scores and a pick-off for Miami (4-5), which was distracted by the Jonathan Martin controversy.
Martin, who has accused teammate Richie Incognito of bullying him, left the team Oct. 31. Incognito was suspended by the Dolphins last week amid an NFL investigation.
The Dolphins managed only 2 rushing yards, setting a team record for futility.
Rishard Matthews corralled both of Tannehill's scoring strikes, finishing with 120 yards on 11 receptions.