After a draw at Norwich City and a home defeat to Brighton & Hove Albion, Ljungberg needed a response and it finally came on the hour.
Until then this was a London derby with a distinct lack of quality or intensity, with Bernd Leno saving well from fit-again Michail Antonio the only real chance before the Hammers hit the front on 38 minutes.
Arsenal could not clear their lines from a corner and Ogbonna shouldered the ball goal-wards before it cannoned off Ainsley Maitland-Niles – called in at the last minute after Hector Bellerin was injured in the warm-up – and into the net.
Declan Rice’s thumping strike was kept out by Leno as West Ham looked to double their advantage, only for Arsenal to show they can provide the goods when it counts.
Martinelli struck his first Premier League goal on his first start in the top flight as he steered in Sead Kolasinac’s cross.
Six minutes later Arsenal were ahead.
Aubameyang spread the ball out wide to Pepe and the club-record signing needed no second invitation to curl home a fine finish before celebrating in front of the travelling supporters.
The Ivory Coast international then provided the assist for Aubameyang, who was just onside as he hit a crisp volley past David Martin.
The win takes Arsenal up to ninth and ends an alarming run of form which cost Unai Emery his job, with Ljungberg still coy on whether he wants to take the reins fulltime.
West Ham are 16th with the pressure sure to mount on Pellegrini.