The visitors were all out for 467 runs to establish a 129-run lead in the first innings over their Sri Lankan hosts, who were bowled out for 338 on the previous day at the P. Sara Oval.
Sri Lanka reached 54-0 at stumps on the third day with both openers Dimuth Karunaratne and Upul Tharanga unbeaten on 25 to reduce the gap to 75 overnight.
Shakib made 116 and received strong support from Mosaddek (75) and skipper Mushfiqur Rahim, who scored 52 as Bangladesh put in one of their most impressive batting performances in their 100th Test match.
Herath -- Test cricket's most successful left-arm spinner -- in the process became only the second Sri Lankan after Muttiah Muralitharan to claim 1,000 first-class wickets.
Also Read
Left-arm spinner Lakshan Sandakan also impressed among the Sri Lankan bowlers, finishing with figures of 4-140.
It was Sandakan who ended Shakib's 131-run seventh wicket stand with Mosaddek when the Bangladesh talisman lofted a catch to Dinesh Chandimal at midwicket.
The visitors lost three wickets in the space of seven balls in the final half-hour on Thursday but Mushfiqur was his usual composed self on Friday morning as he brought his 17th Test half-century with a glorious off-drive off Dilruwan Perera through extra cover.
Mushfiqur survived a close shout for lbw after completing his fifty before Lakmal broke through his defence with an in-swinger that kept low.
Shakib, who rushed to 18 off eight balls on Thursday afternoon, completed his 50 off 69 balls with a single off Lakmal after surviving a run out while on 40.
Mosaddek then added 33 runs with Mehedi Hasan (34) to stretch Bangladesh's advantage before Herath underlined his quality.
Bangladesh's innings ended when Mosaddek was stumped by wicketkeeper Niroshan Dickewella after hitting seven fours and two sixes in his debut Test knock.
Disclaimer: No Business Standard Journalist was involved in creation of this content