(CNN) -- England kept their Cricket World Cup hopes alive after a dramatic 18-run victory over the West Indies.
Needing to win to stay in the tournament, England failed to capitalize on a quick start to their innings, losing a clutch of wickets in the middle period to post a modest total of 243.
The West Indies got off to a fast start themselves and looked well set to clinch their place in the last eight at 222-6 before losing their last four wickets for three runs to hand England an astonishing triumph.
England will be through to the quarterfinals if Group B leaders South Africa beat Bangladesh on Saturday or India beat the West Indies on Sunday.
For the sixth time in the tournament England were involved in a match that twisted one way and then the other, the outcome unclear until right at the death.
Batting first in Chennai, captain Andrew Strauss got England off to a bright start, hitting 31, while Jonathan Trott looked in good touch before he was dismissed for 47.
But England slumped from 121-3 to 151-6 as Ian Bell, Eoin Morgan and Ravi Bopara all went in quick succession. They were grateful to Luke Wright, who hit 44, and an unbeaten 20 from Tim Bresnan to set a competitive target.
In reply Chris Gayle played in trademark fashion, smashing 43 runs off 21 balls and taking 18 off an over from Chris Tremlett before spinner James Tredwell made his mark on his first appearance in the tournament.
He claimed the first three wickets as Gayle, Devon Smith and Dwayne Bravo were all removed. When Bopara then bowled captain Darren Sammy and Devon Thomas, England could scent victory.
Explosive batsman Kieron Pollard fell for 24 but Ramnaresh Sarwan and Andre Russell steadied the ship for the West Indies before Trott thought he had caught Russell in the deep.
But Trott's catch was ruled out by the third umpire, who adjudged he had made contact with the boundary rope after taking the ball. Six runs were given instead.
With the partnership between Sarwan and Russell reaching 72, West Indies needed just 24 more runs to win before the game turned on its head.
First Russell was trapped LBW by Tredwell before fellow spinner Graeme Swann then dismissed Sarwan and Kemar Roach in consecutive balls.
And England's thrilling victory was confirmed when a pinpoint throw from Trott allowed Matt Prior to run out Sulieman Benn.
Swann told the England and Wales Cricket Board's website: "I thought 'It's one of those nights'. We have dug ourselves a hole and we are slowly stepping out of it. It was up there with the Ashes for raw emotion."
Strauss added: "We've just been inconsistent, there's no doubt about it. We haven't played as well as we would like in this tournament, but we have pulled out some results when we really needed them.
"Obviously we are still waiting on other results, but hopefully we have done enough to go through to the last eight."