Roshen Silva remained unbeaten on 70, adding 48 runs with fellow overnight batsman Suranga Lakmal for the ninth wicket to extend its lead on a hard pitch at the Sher-e-Bangla National Stadium. He had eight wickets on debut, including his 3/20 in the first innings.
The veteran spinner, Herath has been a huge contributor to Sri Lanka's change in fortunes after a dismal show against India.
The first Test in Chittagong ended in a draw.
With his four-wicket haul, 39-year-old Herath (415) passed Wasim Akram (414) for most Test wickets by a left-arm bowler.
Seamer Jayed was the second highest wicket-taker in the Bangladesh Premier League (BPL) in 2017, while Mahedi bagged ten wickets at a strike rate of 25.70 in the league.
The visitors added 26 runs from its overnight total of 200-8.
The hosts suffered an early jolt in the mammoth chase when Tamim Iqbal fell as early as the second over, leg-before to Dilruwan Perera with the review failing to come to his aid.
Left-arm spinner Taijul Islam struck twice in his first over of the day to wrap up the Sri Lankans innings.
Brief scores: Sri Lanka 222 & 226 (Roshen Silva 70, Dimuth Karunaratne 32; Taijul Islam 4-36) lead Bangladesh 110 & 57/2 (Mominul Haque 31*; Rangana Herath 1-17) by 281 runs. These are also the 3rd fewest Sri Lanka needed to bowl out a team in an innings with all the top three coming against Bangladesh.