Smith and Marsh put on a record 246 runs for the second wicket, raising Australian hopes of building a match-winning lead in response to Sri Lanka's first innings total of 355 before the veteran Herath put the brakes on with a six wicket haul.
Australia were eventually all out for 379, a lead which was reduced to just two runs by the close of play as Sri Lanka reached 22 at stumps, albeit with the loss of opener Dilruwan Perera who was lbw to Mitchell Starc for eight.
Herath -- who took a hat-trick in the last Test and 10 wickets in the first match of the series -- again brought the hosts back into contention by taking six for 81.
The 38-year-old veteran ran through the Australian tail with help from fellow spinner Dilruwan Perera's two wickets.
Also Read
Smith, who hit 10 fours and a six during his 119-run knock, departed after being stumped off Herath, with the third umpire ruling in favour of the bowler in a close decision.
The marathon partnership between Smith and Marsh was the highest second-wicket stand between the two countries.
The pair ensured that Australia did not lose a wicket in the first session as the visitors took lunch on 235 for one.
Marsh, who was drafted into the Australia side because of his long experience of Asian conditions, survived a dropped catch on 67 early in the morning against Herath but soon took control.
Smith, who resumed the day on 61, reached his 15th Test century to improve on his previous best of 55 in the series.
Mitchell Marsh, who registered his second Test half-century, contributed a useful 53 to help Australia surpass Sri Lanka's first innings score.
Top-ranked Australia are bidding to avoid a series whitewash after losing the first two Tests.
Disclaimer: No Business Standard Journalist was involved in creation of this content