Bobby Zamora bagged a couple of stunning goals to guide Alan Pardew's side through to the next round of the Carling Cup in a nail-biting Hillsborough thriller.
The Hammers were coasting to a comfortable 3-0 victory before basement side Wednesday hit them with two goals in a couple of minutes.
Zamora, desperate to claim a regular first-team spot and making only his second start of the season, grabbed his opener with just 75 seconds on the clock.
Zamora, in space on the left, linked cleverly with midfielder Gavin Williams before finishing clinically with a spectacular left-foot shot into the far corner from 16 yards as David Lucas charged off his line.
Goal hungry Marlon Harewood, with four in two games, powered in a stinging drive which flashed inches wide of the near post minutes later with Wednesday creaking.
Zamora almost snatched a second with Wednesday exposed. Shaun Newton sliced through the Sheffield rearguard with an inch-perfect cross to the waiting West Ham front man whose fierce header was pushed away by Lucas.
The Hillsborough side battled back into the game and found a way through on 27 minutes. Winger Jon Paul McGovern, making his first start of the season, picked out Glen Whelan who fired over the top from 16 yards.
But they were stopped in their tracks on 54 minutes when skipper Christian Dailly climbed above his marker to connect with a Newton corner and finish with a bullet header from eight yards.
Zamora added the third in the 63rd minute as he spotted Lucas off his line and curled the ball over him from 25 yards.
Wednesday bounced back with two in as many minutes. Graham Coughlan headed in a Chris Brunt corner after 76 minutes before David Graham struck with a low drive from 15 yards.
There was more drama in the dying minutes when Graham hit the bar with another piledriver before David Bellion finished off the Hillsborough side six minutes from time after twisting past two markers before finishing from 15 yards.