The 28-year-old Mehidy finished with 5-102 to help Bangladesh dismiss Pakistan for 386 and take a 27-run first-innings lead on a rain-affected third day on Sunday.
Bangladesh then moved to 0-7 before bad light stopped play.
Opener Azan Awais hit 103 on his debut to lead Pakistan's batsmen.
Three other batters — debutant Abdullah Fazal, Salman Agha and Mohammad Rizwan — hit half-centuries to complement him but Mehidy ensured Bangladesh took the lead.
Pakistan appeared set for a big first-innings total after resuming at 1-179.
Azan began the day fluently, hitting two fours in the first four overs to reach 99. He then nudged Nahid Rana to point for a single to raise his century off 153 balls, becoming the 15th Pakistan batter to hit a ton on debut.
But the brilliant knock came to an end when Taskin Ahmed (2-70) made him his 50th Test wicket. In doing so, Taskin became only the third Bangladeshi pacer after Mashrafe Bin Mortaza and Shahadat Hossain Rajib to reach the milestone.
Taskin then sent back captain Shan Masood to trigger a mini collapse as Pakistan lost three wickets for four runs.
Mehidy struck twice, including the wicket of another debutant Fazal, who made 60, leaving Pakistan 5-230.
Salman Agha and Rizwan mounted a strong recovery with a 119-run partnership, which frustrated Bangladesh's bowlers and revived Pakistan's hopes.
But left-arm spinner Taijul Islam broke through with the wicket of Rizwan, dismissing him for 59.
Rain arrived shortly after his dismissal to delay the game for two hours.
Fast bowler Nahid Rana struck immediately after the resumption by getting the better of Salman for his only wicket in the innings.
Salman's dismissal, however, effectively ended Pakistan's hopes of taking an innings lead.
Mehidy then sent back Noman Ali and Shaheen Shah Afridi in consecutive overs to complete his 14th five-wicket haul before Taijul (2-46) wrapped up Pakistan's innings.