Having lost five successive matches, the Windies were only one place above the winless Afghans but Shai Hope, dropped on five, made an assured 77.
Skipper Jason Holder blazed four huge sixes to take their total to 311-6.
Afghanistan were all out off the final ball for 288, 18-year-old wicketkeeper Ikram Alikhil scoring 86 in their highest score batting second in ODIs.
Afghanistan finish their campaign without a victory but they were in contention once again, having lost in the last over to Pakistan and by only 11 runs to India.
They were by no means outsiders against the Windies, having won three of their four completed previous encounters, including both matches in the qualifying tournament in Harare last year.
West Indies end losing streak
The Windies had begun the tournament so encouragingly by bowling out Pakistan for 105, knocking off the runs in 13.4 overs and having Australia 38-4 in their second match, but had deteriorated alarmingly since then, although they were one hit away from defeating New Zealand.
There was hope of a fitting World Cup finale for Chris Gayle, equalling Brian Lara's West Indian ODI appearance record of 295, but it was to come in the field rather than with the bat.
The 39-year-old, who has indicated he would like to play on, bludgeoned only one boundary before another attempted big hit resulted in an edge to the wicketkeeper in the sixth over, leaving him just 10 runs short of Lara's ODI run record of 10,348.
Their younger batsmen proved there is plenty of talent for the future, left-hander Evin Lewis making a composed 58 and man-of-the-match Hope taking advantage of a routine dropped catch at mid-wicket by Rashid Khan to compile his second half century of the tournament.
There may still be criticism of the failure to build major innings, but left-handers Shimron Hetmyer (39) and Nicholas Pooran (58) played in exciting fashion again and Holder struck some enormous blows as 111 runs were added in the final 10 overs of an innings containing 12 maximums.
It was a difficult day in the field for the Afghans, with four chances going awry, as many as they let slip in their other eight matches combined, with skipper Gulbadin Naib bizarrely losing sight of a skied chance in the sunshine.
Although the skipper was dismissed in the second over, any suggestion of a malaise from a side whose only World Cup success was in 2015 against Scotland, was swiftly allayed by a superb partnership of 133 in 24 overs.
Their previous highest total in the tournament had been a modest 247 but Rahmat played some sublime strokes on both sides of the wicket in their first World Cup century partnership, while the purposeful Ikram eclipsed a record held by India legend Sachin Tendulkar to make the highest World Cup score by an 18-year-old.
Sheldon Cottrell, who took perhaps the catch of the tournament against Australia, spilled a routine chance at long-on off Brathwaite in the 25th over when Rahmat was on 57, but Gayle soon swooped forward to dismiss the talented opener at cover, bowler Brathwaite joining him on the turf for some celebratory press-ups in tribute to the veteran's fitness.
Having been unable to defend a total of 321 against Bangladesh earlier in the tournament the Windies were guilty of a missed stumping and mis-fields as the runs continued to flow, but it was Gayle who made the key breakthrough, trapping Ikram lbw in the 36th over with 123 still needed, before Hetmyer's superb throw helped run out an incensed Najibullah Zadran two balls later.
And the West Indies, who drew 2-2 with England earlier this year, did just enough to ensure a second victory as Fabian Allen leaped superbly to end the match with a one-handed diving catch in the sixth successive World Cup fixture won by the side batting first.
'Good to get over the line'
West Indies captain Jason Holder: "It's good to get over the line, we've had some close encounters where we didn't. The batters stepped up today. Evin Lewis and Shai Hope got a good partnership going and 300 on that pitch was good enough to be a winning total.
"We stayed strong with the ball and came through at the end. Overall at this tournament, inconsistency let us down, in all three departments. Our fielding has to improve a lot."
Afghanistan skipper Gulbadin Naib: "It was a good score batting second, so credit to the batsmen and Ikram showed his class today. We also have other talented players back at home.
"I have learnt a lot at this tournament. Fitness is a big issue - the boys are struggling. Also, we can work more on our skills under pressure."
Source: BBC Sports