Henri Anier and John Sutton took advantage of a St Mirren defence that appeared disorganised and uncomfortable while playing a high line to put Motherwell two ahead inside 27 minutes.
But, with Motherwell missing injured centre-back Shaun Hutchinson, St Mirren looked dangerous at corners and Steven Thompson converted one just before the break to give the Buddies hope.
They pressed in the early stages of the second half but Motherwell had chances on the break and appeared to have withstood the pressure.
However, stand-in referee Iain Brines awarded the hosts a controversial penalty for handball by Stephen McManus in the 86th minute.
McManus had blocked Thompson's header at point-blank range but Brines, after some hesitation, decided it was deliberate handball despite the Motherwell defender's protests.
Kenny McLean converted from the spot and Thompson stunned the visitors when he turned and fired home from close range a minute later.
St Mirren had the first chance when Darren McGregor won McLean's corner but Gunnar Nielsen got down well to stop.
Sutton headed just wide from a Craig Reid cross at the other end and Motherwell were ahead in the 16th minute after St Mirren were caught out by a diagonal ball from McManus.
Lionel Ainsworth had yards of space to run into and he squared for Anier to stroke home after Saints goalkeeper Christopher Dilo rushed out unconvincingly.
Saints lost Paul McGowan to injury in the 22nd minute as Gregg Wylde came on, and they missed a great chance to equalise when Jim Goodwin sent a free header wide from six yards following another McLean corner.
Motherwell punished them two minutes later to double their lead after probing for an opening on the edge of the St Mirren box.
The home defence was again stretched as Steven Hammell crossed and Ainsworth volleyed the ball across the face of goal for Sutton to stoop and head home.
The Saints defence was soon exposed again as Reid broke down the right to cross but Jason Naismith got across to cover and divert Sutton's shot wide.
But the hosts continued to look dangerous whenever they got a corner and Thompson headed not far over before he pulled one back in the 41st minute.
McGregor headed back McLean's deep delivery from the right and Thompson swept the ball home with his left foot from 10 yards.
Fourth official Brines replaced Euan Norris as referee at half-time and the pattern of the game changed as well as the home side pushed for an equaliser.
Naismith fired several dangerous balls across the face of goal and Thompson had a series of efforts, including a diving header and a shot on the turn that Nielsen both saved well.
Saints brought on striker Josh Magennis for midfielder Conor Newton but Dilo kept them in the game when he acrobatically tipped Ainsworth's 20-yard strike over the bar.
Saints survived another major scare when Craig Moore beat the offside following Sutton's pass, moments after replacing Anier, but he snatched at his shot when clean through and dragged it a yard wide.
And Motherwell were made to pay for the miss in a dramatic finale as St Mirren climbed above Partick Thistle and consigned Hearts to relegation.