ICC Men's World Cup, Ahmedabad
Afghanistan 244 (50 overs): Azmatullah 97* (107); Coetzee 4-44, Maharaj 2-25
South Africa 247-5 (47.3 overs): Van der Dussen 76* (95), De Kock 41 (47); Nabi 2-35
South Africa won by five wickets
Rassie van der Dussen hit an unbeaten 76 to take South Africa to a tense five-wicket win over Afghanistan in their final World Cup group game.
Gerald Coetzee took 4-44 as the Proteas bowled Afghanistan out for 244 after Azmatullah Omarzai made an unbeaten 97 to rescue the innings from 116-6.
South Africa made hard work of the chase but held their nerve to get over the line in the 48th over.
Defeat officially ended Afghanistan's chances of making the top four.
Jonathan Trott's side needed to win by a highly improbable 438 runs to overhaul New Zealand on net run rate and stay in contention to reach the knockout stages but, after opting to bat first, lost wickets in clusters.
After a steady start, they lost three wickets for four runs to slip from 41-0 to 45-3 and then another mini collapse saw them lose three wickets in four overs midway through the innings.
However, Azmatullah held Afghanistan together with a well-paced knock, biding his time before accelerating late on, but three dot balls to end the innings left him three shy of a maiden international century.
In reply, South Africa, who have dominated sides when batting first but struggled chasing, started well with openers Quinton de Kock and Temba Bavuma putting on 61.
They fell in quick succession but Van der Dussen and Aiden Markram continued the momentum with a 60-run stand.
But every time the Proteas appeared to be cruising, Afghanistan took a wicket to drag them back and when David Miller was dismissed with 62 still needed, the game was in the balance.
Van der Dussen and Andile Phehlukwayo remained composed, though, and having whittled down the runs required, they finished it in a hurry with the latter going six, four, six to seal the win.
With a number of players struggling with slight injuries - most notably Bavuma and Lungi Ngidi -throughout the game, South Africa now have a few days to rest and recover before their semi-final against Australia.
Afghanistan out after tournament to remember
After losing to Bangladesh in their first game, not much was expected of Afghanistan at this World Cup.
For all the talent in their team and the way they had shown themselves capable of competing with the top sides in white-ball cricket, they had yet to do it on the big stage with their only 50-over World Cup win coming against Scotland in 2015.
A month later and they have beaten three former champions in England, Pakistan and Sri Lanka - all in convincing fashion - and comfortably seen off the Netherlands as well.
But for one of the great one-day innings of all-time, they would surely have added Australia to that list and come into this match knowing a win would put them into the semi-finals.
That dream has eluded them this time but any notion of Afghanistan being minnows of world cricket has long since been banished.
The confidence gained from their accomplishments in India should serve them well and with an ICC tournament each year for the foreseeable future, they will have plenty more chances to make history.
'Part of me is dreaming' - what they said
South Africa captain Temba Bavuma: "My leg is sore. I don't know the extent of it yet but I'll have to be fine. I am stubborn, it will have to be fine.
"Part of me is dreaming. We want to be back here [in the final]. But Australia are a big hurdle for us to overcome. We will enjoy today, take the learnings and then get our minds set on that game."
Afghanistan captain Hashmatullah Shahidi: "We gave a good message to the world in this tournament. We played big teams and we fought. The Australia game was in our hands, it was a shock that defeat. There was a lot of learning."
Player of the Match, South Africa's Rassie van der Dussen: "Someone always has to play the anchor role in a chase and I was happy to do that. The platform I had from Temba and Quinton made my job a lot easier, too.
"We do so well batting first and that makes people think we chase badly. We would like to improve our chasing, yes, and I think being in that situation tonight will hold us in good stead."