It was the Accies' first win in six matches, while Livingston are now without a victory from their last four games.
Alan Lithgow put the visitors in front with a thumping header from a free-kick, before Blair Alston equalised for Accies on the stroke of half-time.
Cunningham then kept his cool from 12 yards with six minutes remaining after goalkeeper Ross Stewart was adjudged to have fouled Marios Ogkmpoe as the Accies striker challenged for a header.
Hamilton made two changes from the side that had drawn with St Mirren. Steve Davies came in for the suspended George Oakley for his first league appearance of the season, while Ronan Hughes replaced Will Collar.
Livingston made four changes from the team knocked out of the Betfred Cup by Rangers in midweek. In came Stewart, Lithgow, Jack Stobbs and Scott Robinson for Matija Sarkic, Jack McMillan, Keaghan Jacobs and Lyndon Dykes.
Lithgow had missed the cup tie with a hamstring injury but made an early impression on his return to the side.
Robbie Crawford won a free-kick wide on the right and when Aymen Souda's delivery wound its way to the back post the Livingston captain rose highest to nod his team in front after 15 minutes.
Accies responded with a dead ball of their own, Ogkmpoe stooping to meet Alston's corner but unable to get his header on target.
It was a game lacking in any real quality as both sides struggled to create an opening. Souda almost forged a chance for himself late in the first half, dribbling through some half-hearted tackles but shooting weakly at the end of the run.
Livingston would pay for their profligacy in front of goal when the home side drew level just before half-time. Alex Gogic's long throw-in was flicked on by Davies and, after Livi failed to clear, Alston bundled the ball home from close range.
The second half was as stodgy as the first. Souda tried to do the spectacular to put his side back in front but made a complete mess of his attempted bicycle kick, before setting up substitute Scott TIffoney whose header went over the top.
Substitute Mickel Miller could have won it for Accies but blazed his shot high over the bar before Cunningham spared his team-mate's blushes with a cool penalty down the middle.