Striker Gignac found the back of the net one minute into stoppage time to lift OM up to second place with 41 points from 21 games, one point adrift of leaders Olympique Lyon.
"This team want to give everything. It was not an easy game, we did not always play well but we showed great courage," coach Elie Baup told French TV channel Canal Plus.
The result put OM two points ahead of third-placed Paris St Germain who face a tricky trip to fifth-placed Bordeaux on Sunday.
Lorient climbed up to fourth after goals by Kevin Monnet-Paquet, Enzo Reale and Gregory Bourillon earned them a 3-2 win against second from bottom side Troyes.
Nancy stayed at the bottom of the standings on 12 points, one adrift of Troyes, after they were beaten 2-1 at mid table Toulouse.
At the Stade Velodrome, until Gignac's unexpected winner, OM had been carried by Andre Ayew, who was excluded from Ghana's African Nations Cup squad because of fitness concerns.
A week after losing 3-1 at strugglers Sochaux, OM started well before their game unravelled.
Andre Ayew put the Provence side ahead when he headed home from a Joey Barton cross in the 14th minute.
Montpellier, who are 12th on 29 points, responded swiftly as Emmanuel Herrera beat the offside trap to collect a John Utaka through ball and beat Steve Mandanda with an angled shot three minutes later.
Andre Ayew came close 10 minutes before the interval with a shot from just outside the box that shaved Geoffrey Jourdren's left post.
Montpellier upped the tempo after the break and they were rewarded in the 57th minute.
Utaka controlled Cabella's cross into the box and fired past Mandanda to make it 2-1 for the visitors.
It spurred OM back into action and in the 80th minute, Jordan Ayew latched on to his older brother's fine cross from the right.
In the dying seconds, Gignac, who was nowhere to be seen until then, benefited from a bad clearance by Daniel Congre to run solo towards goal and coolly slot the ball past Jourdren.
Earlier, Sochaux left the relegation zone after Loic Poujol earned them a 1-0 win against Stade Reims.
Sochaux are 15th on 22 points and Reims dropped to 18th on 19 points.
- Sports & Recreation