It was a desperately close call but the former Australia seamer ruled in Sri Lanka's favour.
After tea, left-arm spinner Herath, who finished with an impressive three for 59 in 42 overs, had both Chris Jordan and Stuart Broad lbw to leave England on the brink of defeat at 228 for nine.
Anderson though refused to yield while an unflustered Ali went to a 259-ball century with his 16th four, a leg-glance off Pradeep.
But the 27-year-old Worcestershire all-rounder, unable to get on strike for the last over, could only watch as Anderson sank to his knees in despair following his dismissal. There were more than 20 overs remaining when England No 11 James Anderson came into bat.
But he survived on nought until, off the penultimate delivery of the match, he gloved a well-directed bouncer from Sri Lanka paceman Shaminda Eranga to Rangana Herath at leg gully.
Just as Sri Lanka had staved off defeat by one wicket in the drawn first Test at Lord's, it seemed England might do likewise in Leed