Tim Southee (C) and teammates celebrate the wicket of Younis Khan.— AFP
New Zealand, chasing their first series win over Pakistan in 31 years, had been in trouble at 5-119 before BJ Watling's 49 not out anchored a tail-end revival that lifted the home team to a competitive total.
Southee then enticed the Pakistan top order into making a string of rash shots, seaming the ball away with a mixture of short and full-length deliveries.
In his third over, the New Zealand quick removed opener Sami Aslam for five and Azhar Ali for one. Then, in his fifth over, he dismissed the veteran Younis Khan for two as Pakistan slumped to 12-3.
Azam and Shafiq briefly steadied the innings with a 39-run partnership before Wagner's double strike.He bowled Shafiq for 23 then followed up with a short ball to debutant Mohammad Rizwan which he hooked to Matt Henry on the boundary for a golden duck on his test debut.
Ahmed took a single off the next ball to deny Wagner the hat trick.
At the close, Southee had figures of three for 26 while Wagner had 2-15.
Resuming on 77-2 after rain washed out most of the first day, New Zealand lost three early wickets.Ross Taylor went for 37, adding eight to his overnight score before he was caught flat-footed by Sohail Khan and edged the ball to wicketkeeper Ahmed.
Opener Jeet Raval benefitted from two dropped catches by Aslam, progressing to 55 when he nicked Imran Khan to Rizwan who had taken over at first slip.
Henry Nicholls (13) edged Wahab Riaz to Ahmed to have New Zealand 119-5 when Watling went to the crease.
He added 51 with Colin de Grandhomme to lift New Zealand to 170-5 by lunch, only for de Grandhomme to fall to Imran on the first ball after the resumption of play for 37.
Mitchell Santner contributed 16 to a 33-run stand for the seventh wicket before edging Sohail to Younis Khan in slips.
Southee engaged in an intriguing battle with Riaz which saw the swashbuckling New Zealander ducking, weaving and slashing through a barrage of short-pitched deliveries. He reached 29 off 28 balls before he misjudged a slower ball from Sohail and was bowled.
New Zealand were all out in the third over after tea when Watling ran out of partners and was left stranded one run short of a half-century.
For Pakistan, Sohail took 4-99 and Imran 3-52.