Walsall suffered their first defeat in seven league matches at the hands of Gillingham.
Cody McDonald’s second-minute header put the visitors in front, but Jason McCarthy equalised just seven minutes later.
The Saddlers dominated after the break, but an 86th-minute free-kick from Jay Emmanuel-Thomas earned the Gills all three points.
The result leaves Walsall into 17th place, four points above the relegation zone and five points adrift of the play-offs, with a game in hand.
Walsall made the worst possible start by falling behind inside two minutes, McDonald looping a close-range header over Neil Etheridge after Bradley Dack’s driven cross wasn’t cleared.
With the wind in their sails, Justin Edinburgh’s Gills pressed for a second; Jay Emmanuel-Thomas fired a half-volley wide from 30 yards and Max Ehmer volleyed over from the edge of the box.
Despite their early dominance, the Gills hadn’t kept a clean sheet in the league since April and that statistic wasn’t about to change as the Saddlers drew level on nine minutes.
Baily Cargill’s clearance from Joe Edwards’ scooped pass ricocheted fortuitously off McCarthy, who then finished calmly past Stuart Nelson for his second goal of the season.
Walsall – hoping to extend their unbeaten run to seven league matches - were now in the ascendancy and had a few half-chances to go in front.
Simeon Jackson headed off target from Scott Laird’s cross and a stretching Edwards volleyed over from Erhun Oztumer’s excellent lofted pass.
Josh Ginnelly’s inviting free-kick was then headed over by Kevin Toner at the far post, before a left-footed strike from Oztumer was deflected wide.
Gillingham remained a threat, with Billy Knott flashing an effort over the bar on 31 minutes following some patient build-up play.
Walsall thought they had taken the lead in first-half injury time as Oztumer slipped through Franck Moussa, whose effort deflected over Nelson but Baily Cargill made a miraculous goalline clearance.
Half-time: Walsall 1-1 Gillingham
The Saddlers, who won 3-2 in last season’s corresponding fixture courtesy of a last-gasp Jason Demetriou volley, began the second period brightly. A fizzing 25-yard drive from Oztumer flew narrowly wide, while Edwards dragged a volley the wrong side of the far post.
It was the home side who were doing all of the pressing and on 72 minutes, a Scott Laird header from Ginnelly’s corner was cleared off the line.
Gillingham had offered precious little offensively in the second half, yet with six minutes remaining, they got their noses back in front.
Ex-Gill Jackson fouled Josh Wright 20 yards out and Emmanuel-Thomas stepped up to slam home the resulting free-kick via a deflection.
A Jackson overhead kick then flew wide as the shell-shocked Saddlers looked to muster a response, but the visitors held on to make it back-to-back league wins.
Full-time: Walsall 1-2 Gillingham
Team: Etheridge, McCarthy, Preston, Toner, Laird, Edwards (Bakayoko 88'), Chambers, Ginnelly, Moussa (Cuvelier 86’), Oztumer, Jackson.
Subs not used: MacGillivray, O’Connor, Dobson, Kinsella, Makris.