Two goals inside the opening eight minutes from Ravel Morrison and Matt Jarvis had the Hammers in complete control, but they required a dramatic winner from Ricardo Vaz Te after Craig Noone and Peter Odemwingie had levelled matters.
There were just 19 seconds on the clock when Morrison broke the deadlock, with the rejuvenated midfielder controlling a pass from Jarvis before crashing high past Joe Lewis.
Jarvis then grabbed the home side’s second himself, with luck very much on his side as a blocked shot ricocheted off him and into the back of the net.
There was nothing fortunate about Noone’s strike on the stroke of half-time, as he rifled into the top corner, and Peter Odemwingie completed the Cardiff comeback when netting his first goal for the club on 77 minutes.
Just when it appeared as though the tie would head past the 90 minute mark and into extra-time, Vaz Te popped up for the Hammers to nod home a Jack Collison centre.
There was little sign of the drama to come when Morrison stunned the visitors with a goal straight from their own kick-off in an amazing start to Sam Allardyce’s 100th match in charge.
His first game as Hammers boss two years ago was a home defeat by Cardiff, but any chance of repeat was already looking unlikely.
Morrison collected a through ball from Jarvis and ran at stand-in City skipper Mark Hudson, cutting inside the centre-half before lifting the ball over Joe Lewis and into the net.
It was the former Manchester United youngster’s third goal in four starts for the Hammers, as he continues to soften the blow of record signing Andy Carroll’s absence through injury.
Jarvis doubled the lead following a slick move this time started by Morrison, who spun away from John Brayford and knocked the ball to Modibo Maiga on the byline.
Maiga’s first-time pass found Jarvis in front of goal and despite two Cardiff defenders sliding in to block, the winger’s effort eventually rolled over the line.
And the rampant hosts could, and probably should, have had five or six before half-time.
Vaz Te saw a volley superbly palmed away by Lewis and, from the corner, Collison found the ball at his feet in front of goal but poked it wide.
The much-maligned Maiga, still yet to score this season, then broke clear only to drag his shot across goal, and Collins missed an open goal from three yards after James Tomkins had headed Collison’s cross back into his path.
And then, from out of nowhere, Cardiff pulled one back on the stroke of half-time with their first attack of the game.
There seemed little danger when Noone collected the ball out on the right but the winger unleashed a superb 20-yard curler with his left foot which flew past Hammers keeper Adrian and into the net.
Cardiff looked a different team after the break and Jordan Mutch’s swerving effort produced an unconvincing save from Adrian before, from the corner, former Hammer Nicky Maynard sent an overhead kick straight at the Spaniard.
And the equaliser came in the 76th minute when Mutch drove into the area and crossed for Odemwingie to tap in his first goal since his infamous drive to QPR failed to force a January move from West Brom.
However Vaz Te, who himself requested a transfer in the last window, had the final word when he got his head onto Collison’s cross to send the Hammers through.