Pakistan beat Bangladesh by 94 runs at Lord's for a fourth straight win, hours after it missed out on the ICC World Cup semifinals.
Going to the last four instead is New Zealand, a team Pakistan beat by six wickets last week.
Pakistan secured fifth place and finished on 11 points, the same as fourth-place New Zealand. Both team had five wins but the Black Caps advanced with a superior net run-rate. Bangladesh ended at number seven with as many points.
To reach the semis, Pakistan had to win by an unprecedented 316 runs. It won the toss and finished on 315-9 with Imam-ul-Haq scoring a 100-ball 100 and Babar Azam 96 in 98. That meant it had bowl out Bangladesh for 7 or less, and that equation died in the second over of the chase.
However, Pakistan bowled out Bangladesh for 221 in 44.1 overs to record their fourth win on the trot since suffering defeat at the hands of arch-rivals India.
Friday's result means that New Zealand have joined India, Australia and hosts England in the knockout stage.
Shakib Al Hasan's incredible run at this World Cup culminated with a 64-run knock. He totalled 606 runs with an average of 86.57.
The world number one all-rounder kept Bangladesh interested in the contest but did not get support from his colleagues. Saumya Sarkar (22), Mushfiqur Rahim (16) and Liton Das (32) got starts but could not convert them into impact innings.
Once Shakib was dismissed by left-arm pacer Shahin Afridi, it was over for the 2015 quarterfinalists. Afridi returned career-best figures of six for 35, while leg-spinner Shadab Khan took two wickets.
Earlier, Imam, aged 23, became the youngest Pakistani to score a hundred in a World Cup game but Azam missed out on his second century in the tournament by just four runs.
Imad Wasim's quick-fire 26-ball 43 took Pakistan past 300.
The intent to get that improbable total was missing in opener Fakhar Zaman as he took 31 balls to score his 13.
Imam and Babar too batted in their usual fashion, showing no urgency to get quick runs. Their partnership was broken when Mohammad Saifuddin trapped Azam with a dipping yorker. Azam's knock came off 98 balls, with 11 shots to the fence.
The bespectacled left-handed Imam was joined at the crease by the seasoned Mohammad Hafeez (27) and they raised a 66-run stand for the third wicket.
Imam completed his seventh ODI hundred with a single off left-arm Mustafizur Rahman in the 42nd over of the innings but was soon out hit-wicket.
Hafeez swept a spinner straight to a fielder in the deep. Had it not been for Wasim's knock, Pakistan would have struggled to reach 300.
Since their defeat against arch-rivals India, Pakistan have recorded three wins on the trot. They can wrap up their campaign on a high with a win.
(At The Quint, we question everything. Play an active role in shaping our journalism by becoming a member today.)