At stumps, England were 414 for five in their second innings, a lead of 311 runs.
Bairstow was 82 not out and Ali 60 not out.
England's sixth-wicket duo had added an unbroken 132 runs inside 27 overs ahead of Monday's final day.
Pakistan will now have to set a new record for the Birmingham ground if they are to go 2-1 up in the four-match series, as the most any side have made in the fourth innings to win a Test at Edgbaston is South Africa's 283 for five in 2008.
More From This Section
But openers Alastair Cook and Alex Hales both went early on Saturday as Pakistan took two wickets for no runs in nine balls.
Joe Root (62) and James Vince (42) stopped the rot during a third-wicket stand of 95.
But their run-rate slowed to barely three an over, albeit in the face of sometimes overly-defensive Pakistan field settings.
Leg-spinner Yasir Shah struck either side of tea to dismiss Root and Gary Ballance.
But the middle-order pair's Yorkshire colleague Bairstow, in at 257 for four, scored briskly, just as opposing wicket-keeper Sarfraz Ahmed had done in Pakistan's first innings.
Play resumed Saturday with England captain Cook 64 not out and Hales 50 not out after they had erased a first-innings deficit of 103 with their maiden century stand in 18 innings as a Test-match opening pair.
But Pakistan soon removed both batsmen.
Left-hander Cook (66) pushed out to first-innings five-wicket hero Sohail Khan and a diving Shah held an excellent catch at point.
Root, who made a Test-best 254 in England's 330-run
series-levelling win at Old Trafford, struck two superb fours off Sohail -- a back-foot force followed by a cover-drive.
After lunch, Root pulled Shah for a boundary that saw him to a 108-ball fifty.
But Shah, bowling into the rough outside leg stump, had his revenge when Root, not for the first time this season, mistimed a sweep and gave a simple catch to Hafeez at short fine leg.
Vince, yet to make a fifty in nine Test innings, had been composed in equalling his highest score at this level of 42.
But he too fell in familiar fashion when, flirting outside off stump against the new ball, he wastefully guided Amir to second slip Younis.
But Ali cover-drove Shah for four and, two balls later, he whipped him for another four wide of mid-on.
Bairstow clipped Sohail off his pads and swept Shah for two well-struck fours on his way to a 69-ball fifty.
Left-hander Ali, to chants of 'Moeen, Moeen Ali' from a crowd of more than 15,500, followed Bairstow to the landmark in 64 balls.
Bairstow then extended England's lead beyond 300 when he swept part-time spinner Azhar Ali to the fine leg rope.