Sri Lanka were 214 for four in their second innings at stumps on Sunday's third day, a lead of 106 runs.
Jayawardene was 55 not out and Sri Lanka captain Angelo Mathews 24 not out, with two days remaining to decide the fate of this two-match series following the drawn first Test at Lord's.
Off-spinner Ali, primarily a batsman and in just his second Test, stunned Sri Lanka with two wickets for no runs in three balls to dismiss Kumar Sangakkara and Lahiru Thirimanne.
Earlier, Mathews took his Test-best figures of four for 44, with fellow seamer Shaminda Eranga enjoying a return of four for 93 as England -- 320 for six overnight on the back of Australia-born opener Sam Robson's maiden Test century -- were bowled out for 365.
The pick of the four England wickets that fell Sunday came when all-rounder Mathews produced a superb off-cutter to clean bowl tailender Liam Plunkett.
England's seamers then repeatedly bowled too short.
And when James Anderson did induce an edge from Dimuth Ka