Anderson's goal on his SPL debut and a stunning first for the club from Grainger hauled promoted Saints off the foot of the table and above their opponents.
A first top-flight clean sheet of the campaign will also please Derek McInnes as Accies failed once to seriously test Alan Main.
Marvin Andrews was handed his first appearance since the opening day of the season as Hamilton made two changes.
Former Rangers defender Andrews replaced flu victim Mark McLaughlin, while Grant Evans came in for the injured Simon Mensing.
As well as handing Anderson a first SPL start, Saints recalled Martin Hardie, Liam Craig and the fit-again Steven Milne.
The teams went into the game separated by just one point at the foot of the table, with the visitors knowing a first league win since promotion would see them leapfrog their hosts.
Swirling wind and a sudden downpour greeted the players' arrival on the field, though the latter quickly subsided.
The same could not be said about a lacklustre opening in which neither side was able to fashion a chance of note.
One finally arrived when Trent McClenahan gifted possession to Kenny Deuchar 19 minutes in.
The former Accies striker fed Craig, whose rising near-post drive brushed the outside of the post.
Hardie hit the wall with a free-kick and fired a first-time shot over from 25 yards either side of a 30-yard Marco Paixao blast which was wide.
Graham Gartland was then booked for felling the forward on halfway.
Saints were beginning to edge proceedings and went close again just past the half-hour mark when Milne scuffed Deuchar's left-wing cross just wide under pressure from his marker.
And they could easily have been ahead five minutes before the break but Gartland's left-foot finish after a Craig corner was blocked on the line.
Paixao went close from 25 yards but Saints took a deserved lead on the stroke of half-time when Hardie nodded Craig's corner back across goal and Anderson rose highest to head home from point-blank range.
Hamilton almost scored a freak equaliser eight minutes after the restart when Richard Hastings' corner found its way through a crowd of players and needed Anderson to clear off the line.
James Wesolowski became the second player booked for a tackle from behind on Saints skipper Jody Morris.
Evans' pinpoint sliderule pass then picked out Paixao, who got between two defenders but made a hash of his finish.
Just past the hour mark, home boss Billy Reid made a double substitution, bringing on Leon Knight and Stuart Taylor for McClenahan and Gary Mason.
Opposite number McInnes was forced into a 66th-minute change when Milne was hurt by a Wesolowski tackle, Collin Samuel coming
Hamilton finally began to exert some pressure in their opponents' box but Saints' defence - which had not kept a clean sheet in the league all season - held firm.
The hosts then refused to put the ball out of play after Deuchar was hurt by an Andrews tackle, and Paixao saw his shot blocked trying to score what would have been a controversial goal.
Deuchar was unable to continue and was replaced by Stuart McCaffrey 11 minutes from time.
Knight was not quite tall enough to head Evans' far-post cross on target before Saints put the seal on their victory in stunning style.
With six minutes remaining, Grainger burst forward, cut inside on to his right foot, and unleashed a thunderous drive into the top corner.
Paixao curled a free-kick narrowly wide, Saints withdrew Craig for Filipe Morais, and Evans fired wide with the final kick off the game.