Needing 266 runs for the win, Bangladesh were off a nightmarish start, losing their entire top order by the 12th over.
But Shakib and Mahmadullah then joined forces to add 224 runs between them in 209 balls and snatch victory from what seemed at one point to be also certain defeat.
Shakib scored 114 runs from 115 deliveries, hitting 11 boundaries and a six during his stay in the middle.
Mahmadullah remained unbeaten on 102 runs off 107 balls with eight hits to the fence and two sixes.
This is the highest ever partnership for Bangladesh. The previous highest was a stand of 148 runs off 140 balls by Mushfiqur Rahim and Shakib Al-Hasan against New Zealand in Chitagong in 2014.
By the time Shakib was bowled in the 47th over by an excellent yorker by Trent Boult, Bangladesh were on the verge of victory as they needed just nine runs from 21 deliveries.
Mosaddek Hossain then joined Mahmadullah in the middle to see Bangladesh through to victory.
The team from South Asia crossed the line in 47.2 overs.
Friday's result has given Bangladesh to qualify for the semi-finals. They finish their group engagements with three points from as many matches.
They suffered an eight-wicket in the tournament opener against hosts England. They then earned one point from their washed out second game against Australia.
If England manage to defeat Australia in the final game of Group A on Saturday, then Bangladesh will go through to the knockout stage along with the hosts. Australia have been extremely unlucky as they have notched up only two points so far as both their matches were abandoned due to rain.
The Kiwis on the hand will have to catch the new flight back to New Zealand. They finish a poor campaign with a single from three matches.
Boasting a power-packed line-up of fast bowlers, the Kiwis were expected to do well in the swing friendly English conditions. But their bowling showed surprising lack of bite on Friday despite some early promise.
They earned their only point from their abandoned campaign opener against Australia before going down by 87 runs to England.
New Zealand fast bowler Tim Southee knocked the wind out of the Bangladesh batting in the early stages of the chase by picking up three wickets in his opening three overs.
With Bangladesh struggling at 12/3 in the fifth over, Shakib and Mushfiqur Rahim 21 runs between them before the Kiwi pacer Adam Milne found the gap between the bat and pad of the latter.
That was the only bit of celebration the New Zealand players will manage on the day as Shakib and Mahmadullah got down to business.
Southee was the standout performer in a rather poor bowling effort by the Kiwis with figures of 3/ in nine overs.
Earlier, New Zealand posted 265/8 after electing to bat first at the Sophia Gardens.
Skipper Kane Williamson (57) and Veteran batsman Ross Taylor (63) were the major contributors for the New Zealand cause.
New Zealand got off to a good start as openers Martin Guptill (33) and Luke Ronchi (16) forged a 46-run partnership before Ronchi was caught by Mustafizur Rahman off pacer Taskin Ahmed in the eighth over.
Soon, Martin Guptill was sent packing in the 13th over by speedster Rubel Hossain. Guptill, who faced 35 balls and slammed four boundaries and one six, was adjudged leg before wicket.
Middle-order batsmen Williamson and Taylor then added 83 for the third wicket to stabilise the innings. But just when it seemed good, Williamson was run out by Shakib Al Hasan. Williamson hit four boundaries in his 69-ball knock.
Incoming batsman Niel Broom (36) then added 49 runs in 8.4 overs with Taylor before the latter was dismissed by Ahmed in the 39th over.
Broom, along with new batsman James Neesham (23), displayed some quality cricket as the duo forged a crucial 27-run partnership before Broom was dismissed by offie Mosaddek Hossain.
Lower middle-order batsmen Corey Anderson (0), Mitchell Santner (14 not out), Adam Milne (7) and Tim Southee (10 not out) then failed to step up to the occasion as they failed to reach a handsome score.
For Bangladesh, Mosaddek Hossain scalped three wickets while Taskin Ahmed took two wickets each.
Brief score: New Zealand 265/8 (Kane Williamson 57, Ross Taylor 63; Mosaddek Hossain 3/13) against Bangladesh.
Bangladesh: 268/5 in 47.2 overs (Shakib Al-Hasan 114, Mahmadullah 102 not out; Tim Southee 3/45) against New Zealand 265/8 (Kane Williamson 57, Ross Taylor 63; Mosaddek Hossain 3/13).