Herath said he was unsure if he would lead Sri Lanka beyond the Bangladesh series.
Tharanga made 115 and Dinesh Chandimal scored 50 not out before Sri Lanka, who had taken a 182-run first-innings lead, declared their second on 274 for six at the Galle International Stadium.
The 38-year-old took 6-59, with the tourists all out for just 197 on the final day in Galle, to take his five-day tally to 366 and pass the previous record of 362 set by New Zealand's Daniel Vettori. Chasing 457, the visitors began the fifth day on 67-0 and hoping the weather would aid their cause, but could only survive until the middle of the afternoon session.
Stand-in captain Herath entered the records books when he dismissed Liton Das to claim his 363rd test wicket.
Sri Lanka dismissed Bangladesh for 197 runs in its second innings in the penultimate session of the match to take a 1-0 lead in the two-match series.
For Mendis, it was probably the luckiest day in his blossoming career when he was bowled by a no-ball before opening his account and then went on to make a hundred from where he continued. It came down to how we batted on the final day and the first session was going to be crucial, but we unfortunately lost wickets. However, Tharanga attempted one shot too many and was bowled by Mehedi after missing a slog-sweep, ending the third-wicket association worth 64 runs.
Soumya Sarkar would end as the Tiger's top scoring batsman but couldn't add to his overnight 53 before Gunaratne castled the left hander with a top notch off spinner. Tamim Iqbal was 13 not out.
Herath, who will celebrate his 39th birthday next week, said he would like to finish his Test career with 400 wickets. However, the procession of wickets did not cease as Herath struck twice soon after Bangladesh crossed 100, sending back Shakib and Mahmudullah in the same over to leave the visitors reeling.
Bangladesh captain Mushfiqur Rahim (34) joined forces with Liton Das (35) to hold off Sri Lanka's victory charge for 19-odd overs before the wheels came off.
Brief scores: Sri Lanka 494 (Kusal Mendis 194, Asela Gunaratne 85; Mehedi Hasan 4-113) & 274/6 decl.