Playing on his home ground, wicketkeeper Bairstow hit 140, 10 short of his Test best, before England was dismissed for 298 after lunch on the second day.
Anderson then exploited overcast conditions to take 5-16, helping bowl out Sri Lanka for 91 in 36.4 overs and becoming in the process the sixth leading wicket-taker in Test history.
Fellow paceman Stuart Broad had 4-21, with England's swing and seam movement rattling the Sri Lankan batsmen.
Bairstow is averaging more than 100 in first-class cricket this season and is making the wicketkeeper position his own in the England team, in competition with Jos Buttler.
He reached his hundred thanks to an overthrow, and punched the air in delight before removing his helmet to take in the applause.
It was the 12th time a Yorkshire player scored a test hundred at Headingley.
Chameera then removed Moeen Ali (0) and Stuart Broad (2), before Bairstow and Steven Finn (17) were the last men out. In reply, Sri Lanka lost Dimuth Karunaratne (0), Kaushal Silva (11) and Kusal Mendis (0) in the space of nine balls, and was reeling at 12-3 just before tea. Broad took two of those wickets and Anderson the other, all caught behind by Bairstow to continue his dream day.
"It was difficult to score if you hit the right areas," Anderson said.
In removing Angelo Mathews lbw for a team-high 34, Anderson went past India great Kapil Dev on the all-time list of test wicket-takers on 435. Dusan Shanaka and Herath edged Anderson behind in the next 10 balls, leaving England's most successful test bowler ever with figures of 4-15.
He finished off the innings, too, when Shaminda Eranga clipped a delivery down the leg side for another catch to Bairstow. It was Anderson's 438th test wicket and the 15th and last wicket of the day, with the players taken off after just two balls of Sri Lanka's second innings.
