The result ensured New Zealand, who outplayed Ireland in the first match of the day in Adelaide, booked their final-four spot from Group 1.
The hosts and defending champions Australia now need arch-rivals England to lose their final Super 12 match against Sri Lanka on Saturday to make the semis.
Maxwell's 32-ball 54 lifted Australia to 168-8, a total their bowlers defended by restricting the Afghans to 164-7 at the Adelaide Oval.
Rashid Khan hit an unbeaten 48 off 23 balls to make a fist of the chase and he smashed 16 runs off the final over from Marcus Stoinis to keep the home crowd on edge almost right to the end.
Australia lost skipper Aaron Finch and Tim David to hamstring injuries and dropped pace bowler Mitchell Starc before being invited to bat first.
Naveen-ul-Haq rocked Australia's top order with the wickets of David Warner, for 28, and the returning Steve Smith, for four, in one over. He returned impressive figures of 3-21.
The left-handed Warner came out firing to smash spinner Mujeeb Ur Rahman for three straight boundaries in the second over of the match.
He lost fellow opener Cameron Green off Fazalhaq Farooqi, who took two wickets, in the next over but kept up the attack with Mitchell Marsh, who hit 45 off 30 balls.
Naveen bowled Warner and four balls later the fast bowler trapped Smith lbw.
Marsh attempted to hit back with a six and two fours off Gulbadin Naib but failed to build on it and he fell caught behind to Mujeeb's mystery spin.
Maxwell and Stoinis, who hit Australia's fastest T20 50 -- in 17 balls -- in the win over Sri Lanka, then got going against the Afghan bowlers, only for Rashid Khan to break through.
Stoinis smashed Rashid for a six over mid-wicket but soon departed for 25 as Afghanistan kept chipping away.
Rahmanullah Gurbaz began the reply by dominating the Australian bowlers. He hit Josh Hazlewood for a four and six in the opening over of the chase.
Hazlewood took out Usman Ghani for two and then Gurbaz departed in the third over off Kane Richardson, who replaced Starc for the key clash.
Gulbadin Naib, who hit 39 off 23 balls, attempted to put the chase back on track with a few good hits including a six off Green as Afghanistan reached 72-2 in 10 overs and threatened an upset.
Naib put on a threatening 58-run stand with Ibrahim Zadran, who made 26, but a direct throw from Maxwell in the deep ran out the dangerman at the non-striker's run.
The wheels then came off the chase with Adam Zampa getting out Ibrahim, for 26, and then Najibullah Zadran in the next three balls.
Rashid, coming in at number eight, fought until the end with three fours and four sixes.
Afghanistan bowed out of the tournament without a win and two washouts.