England were were 33 without loss at tea on the fourth day, needing just a further 46 runs to reach their winning target of 79.
England captain Cook was 23 not out at the Riverside, having scored the five he needed to and just the 12th player of all time to score 10,000 Test runs.
Fellow opener Alex Hales was 10 not out.
Victory in this match would see England take an unassailable lead in the three-match series following their innings and 88-run win in the first Test at Headingley.
More From This Section
At 31 years and 157 days he was the youngest player ever to achieve the feat, beating the previous record of India great Sachin Tendulkar (31 years and 326 days).
Earlier, Dinesh Chandimal's excellent hundred denied England a second straight innings victory as Sri Lanka, following on, made 475.
Chandimal's 126 was his sixth hundred in 27 Tests but first outside Asia.
He received excellent support from Herath (61) in seventh-wicket stand of 116.
Chandimal was 54 not out overnight and Milinda Siriwardana unbeaten on 35 after Sri Lanka captain Angelo Mathews (80) and Kaushal Silva (60) initially kept England at bay.
Play began Monday under a heavily grey sky.
Siriwardana had not added a run to his overnight score when he edged Anderson straight to Alex Hales at gully to end a stand worth 92.
Chandimal, however cover-drove Broad for four and late-cut Anderson behind point for another boundary.
The gutsy Herath, primarily a left-arm spinner, then saw Sri Lanka to 350 when he pulled Chris Woakes behind square for four.
Chandimal went into the 90s when he hooked Finn to the fine leg boundary.
An edged boundary off Woakes to third man saw the 26-year-old Chandimal to a 172-ball hundred including 11 fours.
Chandimal ended an excellent session for Sri Lanka in style by cover-driving Woakes for four off the last ball before lunch.
The 38-year-old, who earlier in this match took his 300th Test wicket, eventually fell when Anderson, taking the fielders out of the equation, had him lbw from round the wicket to claim his 450th scalp in the five-day game.
Anderson, already England's all-time leading Test wicket-taker, then bowled Shaminda Eranga.
His return of five for 58 was the 21st time Anderson had taken five or more wickets in an innings his 115 Test-career.
Chandimal's more than five-hour innings ended when he was bowled driving at Stuart Broad.