Rowan Vine hooked home after a 68th-minute corner and Dave Mackay soon forced home to move Saints above the Dons into fifth place in the SPL.
Gregory Tade opened the scoring for the home side in the 33rd minute after a mistake from stand-in centre-back Isaac Osbourne, but Niall McGinn's superb volley had brought the Dons level a minute before half-time.
St Johnstone midfielder Liam Craig had an early header cleared off the line by Clark Robertson, but that was a rare piece of goalmouth action in the first half hour with the wind appearing to cause problems for the players.
Then Tade was gifted a chance to give his side the lead, when Osbourne took a poor touch after Callum Davidson had played a hopeful ball up the left flank and allowed the striker to run through on goal, with the former Raith Rovers and Inverness striker making no mistake, taking the ball around Langfield and stroking home before Osbourne could get back.
McGinn's cross soon took out home goalkeeper Alan Mannus and allowed Rory Fallon a great chance to equalise but Mackay got a touch just before the New Zealand international, who could not divert it home.
Aberdeen equalised out of the blue. Stephen Hughes played the ball down the right to Magennis, who got away from Davidson and sent over a dangerous ball into the heart of the goalmouth. McGinn reacted quickly and was well placed, but the chance was far from easy and he struck a powerful first-time effort into the roof of the net.
Michael Doughty, who signed on loan from QPR, replaced Nigel Hasselbaink in the 64th minute as Saints matched Aberdeen's 4-3-3 formation.
And they soon went ahead following Craig's deep corner. Langfield stayed on his line and Magennis could only head into the air under pressure from Vine, who showed impressive agility to volley into the roof of the net.
Saints doubled their lead two minutes later with Vine again capitalising on hesitancy. The forward chased a ball dinked into the left channel and appeared second favourite - but he hurled himself towards the ball and neither Robertson nor Magennis could clear with Langfield again staying put. The ball broke for Mackay, who fired home from close range.
Aberdeen claimed for a penalty when substitute Hayes hit the ball off Frazer Wright but referee Steven MacLean played on and the visitors could not get back into the game.