Two penalties and a goal from Alfredo Morelos in the final 10 minutes gave Steven Gerrard his first Premiership away win as Rangers manager.
The win also moved them back to five points behind leaders Hearts ahead of a huge week for the Ibrox side.
Ryan Kent scored his third of the season, but Steven Boyd looked to have earned Accies a point.
James Tavernier stepped up twice in two minutes to score from the spot and Morelos added a late fourth.
A third consecutive victory means Rangers have now won on their last seven visits to Hamilton and move up to fifth in the table.
They face Spartak Moscow at Ibrox on Thursday in the Europa League before a League Cup semi-final with Aberdeen at Hampden Park on Sunday.
After a third straight defeat, Martin Canning’s ninth-placed side are left with just six points from their opening nine league games.
Rangers leave it late
With Rangers without their main two strikers, Morelos and Kyle Lafferty, for next week’s semi-final, the focus was on how others in the team could step up and show their potency in front of goal.
The visitors also had to show they could win away from Ibrox domestically, something they had not managed all season.
Kent and Ovie Ejaria, in particular, will be big players at the national stadium as manager Gerrard attempts to lead his team into their first cup final under his watch.
It took 31 minutes though for home Gary Woods to be forced into a meaningful save, the goalkeeper diving low to claim Ejaria’s curling effort.
But Gerrard’s side got their noses in front when Hamilton coughed up possession in midfield. Morelos played a delightful ball through the middle to Kent, who coolly lobbed Woods.
Woods had to tip over another Ejaria effort, but Hamilton were being left to rue missed chances of their own.
With the scores still at 0-0, Scott Martin and Darian Mackinnon both failed to hit the target after gliding in at the back post, while Aaron McGowan headed over from close range.
Even after they fell behind, Mackinnon spurned another glorious chance when he shot over after goalkeeper Allan McGregor had punched the ball into his path.
Rakish Bingham stung the palms of McGregor before James Keatings shot the rebound wide. But, as the hard luck stories were being written, they equalised in stunning fashion.
Boyd was given time and space on the edge of the box and he cracked an effort into the top corner.
However, they still had 10 minutes to hold out. Within six, they had conceded twice; referee Andrew Dallas pointing to the spot after Alex Penny’s handball then after Morelos fell to the turf from a tug on his shirt.
With the final kick of the ball, Morelos blasted in with the Hamilton defence all at sea – his 12th of the season and his fourth in three games.
Win does not hide the cracks – analysis
Willie Miller on BBC Radio Sportsound
“The three goals scored without the penalties were brilliant. No way could you have predicted that Rangers were going to tuck away four at the end of the day.
“It’s madness, absolutely craziness from the centre-back Alex Penny. Not dealing with the cross and then getting himself all tangled with Morelos.
“But, for long periods it was dire stuff from Rangers. It wasn’t inspiring. The tempo was low, they weren’t creating chances,
“It’s a magnificent 4-1 victory but I don’t think it hides the cracks. It was very similar this game to the Livingston game [1-0 defeat].”