Cheikhou Kouyate had haunted his old club with an equaliser after Robert Snodgrass' opener for the visitors, but Chilean Pellegrini looked set to watch his team make it two games without a defeat.
That was until Ayew spun away from two defenders and chipped over goalkeeper Roberto, much to the delight of boss Roy Hodgson.
For Pellegrini, it compounded another day to forget with his captain Mark Noble and team-mate Angelo Ogbonna almost coming to blows in the 53rd minute to highlight the problems at the east London club.
Roberto earned a recall in goal for West Ham, with David Martin and Lukasz Fabianski not fit enough to feature, while Palace's injury problems increased with Christian Benteke out for the next few weeks with a muscle strain.
The most entertaining moment of the first half saw Patrick Van Aanholt sprint into manager Hodgson while trying to control a loose ball. Fortunately neither were hurt.
West Ham supporters started to 'ole' their passes with 15 minutes played, but Snodgrass crucially could not find the unmarked Pablo Fornals in the area four minutes later for what would have been a certain goal.
Palace went close in the 31st minute when Zaha controlled Luka Milivojevic's clearance, raced at Pablo Zabaleta and saw his cross come for Max Meyer. The midfielder found Ayew, yet the Ghanaian scuffed wide from close range when he should have scored.
Michail Antonio then had a couple of efforts but for the fifth consecutive game at Selhurst Park, it was goalless at the break.
Ayew was presented with another opportunity in the 53rd minute when Aaron Cresswell's back pass was short, but Roberto parried behind for a corner.
Before it could be taken, Hammers captain Noble and Ogbonna were involved in a heated discussion with the away skipper forced away from the defender by firstly Cresswell and then Antonio.
Four minutes later and it was long forgotten when Pellegrini's side attacked and Antonio found Snodgrass, who curled home for his third of the season.
Van Aanholt fired wide not long after before he was forced off with what looked a hamstring injury – the same issue he has just returned from – and the London derby had now gone up a notch.
Palace were searching for an equaliser while West Ham wanted a second to put the game to bed, but it was the hosts who levelled in the 68th minute.
James McArthur stood the ball up to the back post and Ayew's header dropped perfectly for Kouyate to volley in against his old club.
Hodgson turned to Connor Wickham with a quarter of an hour left and the ex-Ipswich forward almost marked his first appearance since August 27 with the winner two minutes later.
Ayew sent him through but Roberto saved impressively to his left while Zaha was free too, but replays showed the substitute was offside anyway.
A winner was forthcoming on the stroke of full-time, however, when Ayew produced a moment of magic.
The forward turned away from two West Ham defenders before nonchalantly chipping over Roberto to send Palace up to eighth and leave the Hammers just above the bottom three.