Ten-man St Johnstone suffered a Paisley sickener to drop to third bottom of the Premiership.
Despite losing Jason Kerr to a straight red card on the stroke of half-time, Saints were less than 10 minutes away from victory thanks to goals from Stevie May and Scott Tanser.
But two balls into their box and two headers turned a fighting win into a dispiriting defeat.
Saints struggled to get a strong foothold in the game in the early stages of the first half but they took the lead midway through it.
Joe Shaughnessy put his hands on David Wotherspoon as he ran across him in the box and referee Steven McLean pointed to the spot.
May’s penalty wasn’t his most convincing he has ever taken but the power was enough to take it past Jak Alnwick.
The hosts equalised on 38 minutes when a wind-assisted ball into the box from the right was only half-cleared and Ethan Erhahon was quickest to react just inside the box and finish well with his first-time strike.
There was a double twist late in the opening period, first when Tanser guided a right foot shot home from 18 yards in 44 minutes and then moments later when Kerr was red carded for a foul on Ilkay Durmus.
That the Saints captain made a mistake in going to ground was beyond doubt but it should have only been a yellow.
It was the team with 10 men who nearly scored next, with Liam Gordon’s glancing header at the near post tipped over the bar by Alnwick.
Just as it was looking as if Saints were going to hold on for the win, St Mirren grabbed an equaliser through a back post Lee Erwin header on 82 minutes.
There was worse to come, though.
St Mirren were awarded a free-kick on the right, from which Jon Obika was able to head home totally unmarked.