In a topsy-turvy day's play in Dhaka, opener Tamim made a quickfire 104 off 147 balls while Mominul Haque scored 66 as the hosts surged to what should have been a commanding score of 171 for one.
But Tamim's dismissal, lbw to Ali, sparked a spectacular collapse, as Bangladesh lost their last nine wickets for 49 runs with seven of their batsmen failing to make double figures.
England then suffered their own mini-collapse as they were reduced to 50 for three before rain brought an early end to proceedings, 11.3 overs before the scheduled close of play at the Sher-e-Bangla National Stadium.
Cook, who equalled Michael Atherton's record of 54 Tests as England captain at the start of play, was initially given not out but the decision was overturned after Bangladesh reviewed.
More From This Section
Mehedi then dismissed Gary Ballance for nine, the batsman's third single figure score of the series, to bring Bangladesh right back into the game, with England still trailing them by 170 runs.
Ali was unbeaten on two at the close, leaving him centre stage at the start of day two of the Test after his starring role in the drama of the opening day.
Tamim smashed 12 boundaries but he became Ali's first victim of the day shortly after driving the same bowler for back-to-back fours through cover to bring up his third century in six Tests against England.
It ended his 170-run stand with Mominul, who had also looked solid until he too was bowled by Ali for 66.
The all-rounders Ben Stokes and Chris Woakes shared the other five wickets as the tourists tightened the noose on a Bangladeshi side still reeling from the disappointment of losing the first Test by just 22 runs.
But the rapid succession of wickets in the afternoon session pointed to Bangladesh's continued frailty in high-pressure situations, severely damaging their hopes of squaring the two-match series.
England made two changes to the team that won the first Test last Monday in Chittagong, with left-arm spinner Zafar Ansari handed a Test debut for Gareth Batty and paceman Steven Finn coming in for Stuart Broad who has been rested.