At lunch, England were 243 for eight.
Bairstow was unbeaten on 103- his second Test century and first in England following the Yorkshireman's 150 not out against South Africa at Cape Town in January.
But there was no maiden Test century for Alex Hales, who fell for 86.
Hales and Bairstow, dropped on 70, extended their overnight partnership to 141.
But the opener's dismissal sparked a collapse that saw England lose three wickets for nine runs as they slumped from 224 for five to 233 for eight.
Bairstow was still on 97 when No 10 batsman Steven Finn, three not out at lunch, came in.
Finn, backing up too far, might have been out had Kaushal Silva's throw hit the stumps after Bairstow played the ball to extra-cover.
But instead the ball went for three overthrow runs which allowed the 26-year-old Bairstow to complete a 145-ball hundred including 10 fours and a six.
England resumed on 171 for five, having collapsed to 83 for five after losing the toss on an overcast first day.
Hales was a Test-best 71 not out, with Bairstow unbeaten on 54 after Sri Lanka medium-pacer Dasun Shanaka had taken three top-order wickets on Test debut.
Bairstow, came into this match in superb form with 533 County Championship runs at an average of 88.
The busy wicket-keeper was soon into his stride, with the best of his shots Friday an on-driven four off Shanaka.
It was not long before he had outscored Hales, who was 43 not out when Bairstow came in.
Bairstow, however should have been out when Pradeep dropped a caught and bowled chance off a checked drive.
Sri Lanka also gave Hales a life when on 82 he edged Sri Lanka captain Angelo Mathews to second slip where a diving Dimuth Karunaratne dropped the sharp catch.
Hales's near five-and-a-half hour innings ended on 86 when, losing patience, he lofted left-arm spinner Rangana Herath to deep extra-cover.
He faced 206 balls, including 12 fours.
Fast bowler Dushmantha Chameera then struck twice, with Moeen Ali caught at short leg for nought - the third duck of the innings - before Stuart Broad played on. – AFP
Original source: Bairstow ton salvages English pride