And this 3-2 second round away victory means incoming manager Alan Pardew will be in charge when Hammers bid to reach the fourth round next month.
West Ham went to Wales still smarting from Saturday's 2-0 defeat at Gillingham - Trevor Brooking's first as caretaker manager - and it looked as though there would be more misery after the prolific Robert Earnshaw opened the scoring for the Bluebirds after 12 minutes and then netted his second - his 13th in 10 games - within 25 minutes.
Hammers deserved nothing more at that stage after surrendering too much possession, especially in midfield, where the trio of Kevin Horlock (centre), Anton Ferdinand (right) and Matt Etherington came under the cosh and lacked bite against their more buoyant first division rivals. When the visitors did have the ball, they created little and gave it back to blue shirts too easily.
Ian Pearce slipped as Earnshaw slammed home his second goal from the edge of the box, but Wayne Quinn won't have been happy with his flimsy challenge as Bowen strolled round him, reached the byeline and pulled an inviting cross back for Earnshaw's first - a tap-in from close range.
The turning point of the match came when referee Andy D'Urso awarded a debatable penalty just seconds before half-time. Etherington put David Connolly clear but as the striker lined up a shot six yards out, on the left side of the box, he missed his kick and went to ground under the challenge of Tony Vidmar. Sky TV replays showed there was no contact - Vidmar missed both ball and Connolly - but D'Urso saw enough to convince him that it was a foul.
Defoe drove the spot kick low and hard to keeper Margetson's right to reduce the deficit to 2-1 and give Hammers renewed hope at the start of a second half which they dominated. David James had precious little to do as Hammers showed their superior class and peppered the City goal with a flurry of shots.
The equaliser came on 64 minutes when Defoe showed great balance to ride a tackle from Boland before striking a shot from 25 yards that flew into the net after glancing off defender Gabbidon. A harsh blow for Cardiff but it was one-way traffic for most of the second half as Hammers tightened their grip on the game.
Defoe was a constant threat and might have completed his first senior hat-rick for the club sooner than he did. One left-foot volley flashed wide of the far post, while on another occasion he attempted a short pass to set up Neil Mellor when he might have elected to shoot from close in on the left.
Mellor's struggle for a goal continues and he saw another good chance slip away when his follow-up effort from Margertson's original save was held by the brave keeper.
Mellor was replaced by Richard Garcia, who soon got into the action and after blazing one shot high and wide, forced another fine save from the Cardiff No.1 when he seemed set to score with his left foot.
Cardiff's only reponse to the claret and blue onslaught was a volley by sub Campbell that cleared James' crossbar in the 85th minute.
But extra-time was the least Hammers deserved for their second half domination and no-one inside Ninian Park was surprised when Defoe emerged again as the main difference between the sides. Ferdinand, growing into his unfamiliar midfield role, picked out a pass to Connolly on the edge of the area and when the Irish international showed subtlety to lay off his first-time pass to Defoe, the hat-trick hero showed his class again with a body swerve and sizzling shot that won the tie in the 89th minute.
Defoe told Sky: "I've scored a few braces but this is my first senior hat-trick and I'm well pleased. It's different from the Premiership but I've just got to keep my head down."
Two ex-Hammers were on hand to offer their views from the Sky studio. Bobby Gould said: "Defoe was virtually asked to go out and play outside-left for the team and did so without sulking. Well done to Trevor Brooking for his man-management.
On the penalty, Gould added: "The referee couldn't see that Vidmar didn't make contact with Connolly - and he can only give what he sees."
Iain Dowie said: "It wasn't a penalty and shouldn't have been given because Vidmar missed Connolly and the ball.
"Defoe was a revelation in the second half and he should be in the England reckoning."
Trevor Brooking said: "We had a sloppy first 10-15 minutes and it was a poor first goal to concede. We worked hard in the second half and Jermain, who was criticised for getting himself sent off at the weekend, responded very well. We showed a lot of spirit and character to come back and I was absolutely delighted for the lads.
"We didn't have a good day at Gillingham and after going two down here, it would have been easy to have wilted.
"In my role as caretaker manager, we've not often been behind, so this was probably the most satisfying result."
Cardiff boss Lennie Lawrence added: "It looks as if the ref has made a genuine mistake but we can't hide behind that. We were still 2-1 up but our standards dropped in the second half and allowed them to get too much on top. Defoe's third goal was sheer quality."
The draw for the Carling Cup third round takes place, live on Sky, at 5.15pm this Saturday.
By Tony McDonald
Minute-by-minute:
9 - Etherington finds a yard of space after collecting from Ferdinand 25 yards out, but drags his effort wide of the target.
12 - GOAL. Cardiff take the lead as Bowen skips easily past Quinn on the right and composes himself in the box before slipping the ball across for Earnshaw to tuck home from six yards out. Cardiff City 1 West Ham United 0.
23 - Quinn's deep cross from the left is met at the far post by Ferdinand, who powers a header just wide.
25 - GOAL. Cardiff double their lead, and again it is Earnshaw. The Welsh striker collects a ball on the left and cuts inside before curling a superb effort past the despairing dive of James and inside the far post. Cardiff City 2 West Ham United 0.
27 - Hammers almost hit back immediately as Connolly wins possession on the left and cuts into the area before firing in a fierce shot that is punched out by Margetson. Defoe's follow-up effort is deflected wide for a corner.
32 - Earnshaw's attempts for a quick-fire hat-trick almost pay-off, but this time his shot sails just over the bar.
36 - Defoe's shot on the turn is blocked, and the young striker then collects a throw-in from Repka before firing in a low left-foot effort that is comfortably held by Margetson.
37 - Earnshaw almost grabs a third as he races on to a long ball, but James races out to deny the striker in the nick of time on the edge of the penalty area.
43 - Defoe beats two men as he surges towards goal but, rather than pick out the better-placed Ferdinand, opts to shoot instead and again sees his effort held by Margetson.
44 - Hammers are given a lifeline on the stroke of half-time, as Connolly is played through but sent crashing to the ground in the penalty area by a defender. Referee D'Urso immediately points to the spot.
45 - GOAL! Defoe steps up to slam the ball low to Margetson's right. Cardiff City 2 West Ham United 1.
Added time: 1 minute.
Half-time: Cardiff City 2 West Ham United 1.
Second Half:
49 - Mellor's low shot from the edge of the penalty area is deflected wide for a corner.
58 - Etherington's deep, hanging cross from the left is met by Mellor, who beats Margetson to the ball but can only direct his header wide.
61 - Dailly booked for foul on Boland.
64 - GOAL! Hammers draw level, with Defoe grabbing his second. Cutting in from the left, he turns past a defender before firing in a shot from 25 yards out that takes a wicked deflection and wrong-foots Margetson to bounce into the net. Cardiff City 2 West Ham United 2.
65 - Cardiff almost restore their lead immediately, as Bonner's low drive flies inches past the post.
67 - Defoe is looking favourite to beat Earnshaw to the match-ball, as he brilliantly controls a long ball into the penalty area and instantly fires a left-foot volley just wide of the target.
74 - Substitution for Cardiff, Gordon replaces Thorne.
80 - Horlock's fierce low drive from the edge of the penalty area is well held by Margetson.
81 - Substitution for Cardiff, Campbell replaces Bowen.
81 - Substitution for West Ham, Garcia replaces Mellor.
84 - Garcia almost becomes an instant hero as he turns inside a defender and unleashes a blistering left-foot shot, but Margetson superbly turns the effort round the post.
85 - Another sub, Campbell, goes close himself with a dipping volley that just clears James' crossbar.
89 - GOAL! Defoe completes his transformation from villain to hero by snatching what must be the winner with two minutes remaining. Collecting a neat lay-off from Connolly, he turns past one defender before hammering home a superb left-foot shot. Cardiff City 2 West Ham United 3.
Added time: 2 minutes.
Full-time: Cardiff City 2 West Ham United 3.
Match report to follow...