Martin Guptill scored 105 as New Zealand faced and only just surmounted the stern test it wanted before the knockout rounds of the Cricket World Cup, beating Bangladesh by three wickets on Friday to finish pool play unbeaten.
Mahmudulluh made his second consecutive century - 128 not out from 123 balls - as Bangladesh reached 288-7 after being sent in and stand-in captain Shakib Al Hasan sprang a carefully-planned trap when his left-arm spin captured the wickets of New Zealand's leading batsmen Brendon McCullum (8) and Kane Williamson (1).
New Zealand was under pressure at 33-2 in its largest run-chase of the tournament, was revived by Guptill but faltered again and scraped home with Daniel Vettori (16) and Tim Southee (12) at the crease and seven balls to spare.
Once again, New Zealand made hard work of a run chase, albeit a testing one. After Guptill's century and Ross Taylor's 56, Grant Elliott and Corey Anderson both made 39 to edge New Zealand toward its target before the final act was left to Vettori and Southee.
New Zealand needed 26 off the last three overs, lost Anderson after he had hit a six, then negotiated the last tense steps to victory through Vettori's big hitting and cool head.
Bangladesh will now face India in a quarterfinal in Melbourne on Thursday and will go into that match with some confidence after a solid and well-rounded performance. It posted the highest total of any of New Zealand's six pool opponents, beating Sri Lanka's 233 in the tournament's opening match, and became the first team New Zealand hasn't been able to bowl out in its six pool matches.
New Zealand's quarterfinal won't be known until Sunday when the last two matches of the pool stage.
The match was a test for both teams in many ways. Bangladesh was without its inspirational captain Mashrafe bin Mortaza who was rested either for a slight sore throat or, more likely, for strategic reasons. Mortaza was fined 40 percent of his match fee when Bangladesh failed to bowl its overs in the required time during its historic win over England on Monday. He faced the possibility of a one-match suspension, which would take him out of the quarterfinals, if Bangladesh erred again against New Zealand.
New Zealand was also forced to make a change to its lineup for the first time in six matches because of a shoulder injury to fast bowler Adam Milne. Mitchell McClenaghan struggled in his first match in six weeks and conceded 68 runs from eight overs.
Bangladesh also had to endure a torrid start as the ball, with Trent Boult taking two wickets to regain his place as the tournament's leading wicket-taker with 15 and Mahmadullah went to crease at 27-2 in the 10th over.
He was dropped on 1 by Corey Anderson but carried his bat for the remainder of the innings, following his historic 103 against England - the first century by a Bangladesh player in a World Cup match.
He first rebuilt the innings in a 90-run partnership for the third wicket with Soumya Sarkar (51) then had a partnership of 78 from 48 balls with Sabbir Rahman (40). Mahmadullah's innings, compiled at a strike rate of 104, contained 12 fours and three sixes.
McCullum has the highest strike rate of any batsman at the tournament but Bangladesh suspected he might struggle against spin at the start of the innings. Shakib tested that theory by taking the new ball and removed both McCullum and Williamson with his slow left-armers. McCullum holed out to long off and Williamson lasted only two balls.
Guptill found form with a century from 88 balls. He was 99 when he had a severe attack of cramp but recovered and pulled the next ball for a single.
New Zealand was 164-3 when Guptill was out in the 31st over after facing 100 balls and adding 131 with Taylor for the third wicket. Taylor edged to his half century from 81 balls and was out for 56, leaving the final work in the hands of the lower order.
Scorecard: New Zealand vs Bangladesh