Perth striker Sam Parkin had given the visitors the lead before the break with a header after 19 minutes of what had been a dire first half.
Former Saints midfielder Simon Mensing levelled from the spot in the 67th minute after St Johnstone defender Michael Duberry had handled a Dougie Imrie corner inside his own box and it looked like a share of the spoils were on the cards.
However, in the 86th minute, after Saints substitute Liam Caddis, on for Collin Samuel, was fouled by David Buchanan, Grainger drove the 25-yard free-kick past a ruck of players and into the far corner of the net.
The visitors started the game the more positively but it was all rather haphazard as both sides battled for dominance.
Amid all the huffing and puffing, in the 12th minute, Accies midfielder Jim Goodwin had an effort from 25 yards which Saints keeper Graeme Smith saved easily.
At the other end moments later, Perth striker Samuel drove wide of the target with a wild swing at the back post.
There were only sporadic moments of excitement and in the 18th minute St Johnstone midfielder Chris Millar battled well to win possession but drove just over from 25 yards.
But a minute later the Perth side took the lead when Liam Craig’s accurate cross from the left found Parkin unmarked and he steered his header from 12 yards past the helpless Hamilton keeper Tomas Cerny.
The home fans growled as the loss of the goal failed to spark their side in to life and it was St Johnstone midfielder Murray Davidson who was next to have an effort on goal, although his volley from 25 yards sailed high over the bar.
Saints overall looked sharper and eager but some slack play in defence had Smith racing quickly from his goal to block Nigel Hasselbaink’s drive.
Then, in the 35th minute, Accies midfielder Gary McDonald headed wide a good cross from Buchanan who did well to reach a Jon Routledge pass down the left.
However, this was no concerted fight back from the home side who failed to test Smith again before the break.
Two minutes after the break Accies had a half-hearted claim for a penalty when Imrie went down in the box after clashing with Duberry, whom he had just robbed, but referee Mike Tumilty was not impressed.
But at least there was some encouragement for the home side and moments later Imrie forced Smith in to a save with a toe-poke from 14 yards after again racing down the right.
Tumilty had to have words with Accies boss Billy Reid, who was becoming increasingly frustrated at some of the decisions he felt were going against his team.
But in the 56th minute there were penalty claims from the visitors when Parkin clashed with Hamilton stopper Mark McLaughlin as he bid to get on to the end of an Alan Maybury cross but again Tumilty stood firm.
However, there was a penalty awarded in the 67th minute when Duberry inexplicable handled an Imrie corner and Mensing side-footed the spot kick confidently in to the corner.
As the game became increasingly tetchy, Hasselbaink had a decent chance in front of goal in the 76th minute but failed to connect properly allowing Smith to make a relatively easy save, the Saints keeper having to make a better save from Flavio Paixao moments later.
If anything Hamilton were now looking the more likely winners but Grainger’s free-kick had the home fans slumped back in their seats.