Pakistan all-rounder Mohammad Hafeez has announced his retirement from Test cricket after an indifferent run of form in the series against New Zealand.
Earlier, Azhar and Shafiq frustrated New Zealand's search for early wickets as they took Pakistan to 224-3 at lunch.
The ongoing third Test against New Zealand in Abu Dhabi, in which Hafeez made a first-innings duck, will be the 38-year-old opener's last as he focuses on limited-overs internationals.
Realistically, NZ are still up against it, as legspinner Yasir Shah seeks to become the quickest bowler to take 200 test wickets with one more scalp.
Somerville then had Azam and Hasan Ali (nought) bowled and wrapped up the innings by getting Sarfraz caught at mid-wicket.
The Black Caps don't have to look far for good omens as they fight for victory in the third and deciding test against Pakistan.
APNew Zealand's Will Somerville and teammates celebrate the dismissal of Pakistan's Azhar Ali
Paceman Shaheen Shah Afridi trapped Jeet Raval for a nought while leg-spinner Yasir Shah had Tom Latham caught for ten, his 199th wicket in Tests. He struck the first ball of the day from Trent Boult for four and ensured Pakistan progressed at a lively pace after two days of grind from both side's batsmen.
He swept with aplomb against Ajaz Patel and had no troubles against the pacemen while Shafiq was content to play second-fiddle after Pakistan began the day at 139-3 in reply to NZ's 274.
Shafiq completed his century with a single off Somerville but fell leg before wicket off Patel in the next over.
Shafiq's 12th test century off 259 balls included 14 fours as he used his feet well against the spinners and played some delightful cut shots against seamers.
Kane Williamson's defiant half-century led New Zealand to 112-4, reaching a resilient 55 off 86 balls.
Shah was run out in freakish fashion when he lost his right shoe while going for the second run with captain Sarfraz Ahmed (25).