Pellegrini was fired after the final whistle, with West Ham only a point and a place above the relegation zone having won just twice in 14 matches
Gray, one of nine new faces in a much-changed Leicester lineup, had a penalty saved by Lukasz Fabianski after 12 minutes after the goalkeeper clattered into Kelechi Iheanacho in the area.
The second-placed visitors did eventually take the lead in the 40th through Iheanacho, who stooped to head in from close range, only for Pablo Fornals to equalize on the stroke of half-time.
Gray made amends for his earlier mistake when he ran onto Ayoze Perez’s through pass and curled the ball inside the near post in the 56th, handing Leicester a first league win in four games and inflicting a fourth straight home defeat on West Ham.
Pellegrini’s decision to substitute record signing Sebastien Haller, rather than give him some support in attack, was met with loud jeers and there were also boos at the full-time whistle.
The former Manchester City and Real Madridmanager lasted 18 months at West Ham.
“It is with great disappointment that we’ve had to make this decision,” West Ham co-chairmen David Gold and David Sullivan said in a statement. “Manuel is a gentleman and it has been a real pleasure to work with someone of his caliber.
“However, it has become clear that a change is required to get the club back on track in line with our ambitions this season. We felt it was necessary to act now in order to give the new manager as much time as possible to try and achieve that goal.”