England were 200 for five at tea, having been 74 for four.
Bairstow was 55 not out and Ali, dropped twice, 52 not out.
England's sixth-wicket pair had so far added 90 -- still some way short of their match-turning stand of 152 in a 141-run win in the third Test at Edgbaston -- but significant all the same.
Recalled paceman Wahab Riaz, who struck twice before lunch as England slumped to 74 for four, dismissed Gary Ballance for Pakistan's lone wicket of a session where England scored 108 runs in 26 overs.
More From This Section
Pakistan had an early breakthrough when Alex Hales (six) clipped Mohammad Amir off his pads and was caught by Yasir Shah, diving forward at square leg.
Hales stood his ground and the umpires called in television official Joel Wilson, who eventually decided he had no evidence on which to overturn his on-field colleagues' initial 'soft signal' of out.
Cook (35) had added just one run after being dropped when he played on, under-edging a pull off Sohail Khan.
Pakistan made two changes to the side beaten at Edgbaston, bringing in debutant batsman Iftikhar Ahmed for the struggling Mohammad Hafeez and recalling Riaz in place of fellow left-arm quick Rahat Ali.
James Vince was then left waiting for his first fifty in
Test cricket when, on one, he nicked a better, straighter and rising, Riaz delivery straight to Ahmed.
Riaz had taken two for none in four balls.
England had added just two runs to their lunch score of 92 when Bairstow, on 13, square cut Riaz to backward point only for South African umpire Marais Erasmus to correctly call no-ball.
- Azhar errors -
================
But Pakistan, whose close catching has proved fallible this series, then saw Azhar twice drop Ali on nine and 15.
Azhar should have held the first chance in the slips, off an understandably frustrated Amir.
Azhar -- now at short leg -- was the unable to hold a far harder catch following Ali's powerful whip off leg-spinner Yasir Shah.
Bairstow went on to complete a 73-ball fifty including seven fours.
Ali who lofted occasional spinner Iftikhar over long-on for six, then reached the landmark in 77 balls.
Players and officials all wore black armbands in memory of the more than 70 people killed during an explosion at a hospital in Quetta, south-west Pakistan, on Monday.
There was further sadness, for the tourists especially, when it was confirmed after lunch that Pakistan's Hanir Mohammad, one of cricket's greatest batsmen, had died aged 81 on Thursday after a prolonged illness.