Ali's 108 was the cornerstone of England's 328 all out.
And there was still time in the two overs before stumps for Stuart Broad to have Sami Aslam lbw for three.
Pakistan ended the day three for one, with both Azhar Ali and nightwatchman Yasir Shah yet to score a run between them.
England, 2-1 up in the four-match series and bidding to become the world's top-ranked Test side, collapsed to 74 for four before lunch after captain Alastair Cook won the toss.
More From This Section
But Ali, missed on nine and 15, turned the tide in a fifth-wicket stand of 93 with Jonny Bairstow (55) that followed their key partnership of 152 in England's 141-run win in the third Test at Edgbaston last week.
The off-spinning all-rounder also added 79 in just 86 balls with Chris Woakes (45).
Sohail Khan took five for 68 -- his second five-wicket innings haul in as many Tests -- including a burst of three for six in 13 balls that reduced England to 286 for nine.
But last man James Anderson stuck in for long enough to allow Ali to go to a third hundred in 30 career Tests in style by dancing down the pitch and lofting leg-spinner Shah for six high over mid-wicket.
Off-spinning all-rounder Ali's second Test hundred of the season, following his 155 not out against Sri Lanka at Chester-le-Street in May, took 140 balls, with 12 fours and two sixes.
Ali was last man out when he was caught in the deep by Shah hooking at Sohail, who kissed the pitch in celebration of his fifth wicket.
- Sohail sparks collapse -
==========================
England were 69 for one when left-hander Cook (35) played on after under-edging a pull off Sohail.
Pakistan made two changes to the side beaten at
Edgbaston, bringing in debutant batsman Iftikhar Ahmed for Mohammad Hafeez and recalling Riaz in place of fellow left-arm quick Rahat Ali.
Riaz, who took five wickets on his Test debut during Pakistan's four-wicket win at The Oval in 2010, had Joe Root out for 26 as he edged a wide but lifting delivery outside off stump to wicket-keeper Sarfraz Ahmed.
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.
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 Amir.
Azhar -- now at short leg -- was the unable to hold a far harder catch following Ali's powerful whip off Shah.
Players and officials 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.
After lunch, it was confirmed that Pakistan's Hanif Mohammad, one of cricket's greatest batsmen and the original 'Little Master', had died aged 81 after a prolonged illness.