Pakistan succumbed to debutant spinner Ajaz Patel's five-wicket haul and lost the first test against New Zealand by four runs on Monday.
Needing 139 more runs with all 10 wickets in hand on the fourth day, Pakistan were bowled out for 171 with Patel taking 5-59 and Wagner claiming 2-27.
Azhar Ali (65) and Asad Shafiq (45) seemed to have carried Pakistan to victory with an 82-run fourth wicket stand before Wagner broke through just before lunch by having Shafiq caught behind.
Babar Azam's (10) run out in a mix-up with Azhar triggered the collapse as Pakistan lost their last six wickets for only 24 runs.
Azhar was the last man to be dismissed when Patel had him leg before wicket as New Zealand recorded their narrowest test victory in terms of runs.
"My game plan was very simple, we as a unit believed we can do something special here and everything went our way," said Patel, a 30-year-old left-arm spinner. "Lot of credit goes to the environment around us because we keep it very relaxed."
New Zealand, which conceded a 74-run first innings lead, was bowled out for 249 in its second innings, giving the visitors an overall lead of 175 runs.
New Zealand fought to successfully defend the second smallest target in its test history with Patel and Wagner making the run-chase difficult for Pakistan on a wearing wicket.
Azhar shielded No. 11 batsman Mohammad Abbas for 7.4 overs to get the last precious 12 runs before he was outdone by Patel's guile with Pakistan narrowly falling short.
Azhar's third half century in six test matches this year featured five boundaries and came off 136 balls.
"We knew the surface was not easy and we saw this throughout from both sides. It's a great advertisement for test cricket that it came down to the wire," New Zealand captain Kane Williamson said. "It's a fantastic game of cricket to be part of and anyone who watched it here appreciated. From our prospective we ended on the right side, we showed the fighting attitude."
Wagner, who bowled an unchanged 13-over spell with lot of energy, also had Yasir Shah caught in the slips as Pakistan lost five wickets for 17 runs after lunch.
Pakistan had a horrible start after resuming the fourth day on 37-0.
Williamson's early use of the spinners worked well for New Zealand, with Patel having overnight batsman Imam-ul-Haq (27) lbw off a delivery which turned sharply into the left-hander and caught the batsman on the backfoot.
Shafiq and Azhar then took the pressure off by rotating the strike frequently with singles and twos while waiting for the loose deliveries to get boundaries.
Shafiq, playing in his 64th test match, reached a milestone when he was on 24 as he completed 4,000 runs in test cricket before New Zealand turned the tables after lunch.
"It's very disappointing to lose it," Pakistan captain Sarfraz Ahmed said. "When the partnership (between Azhar and Shafiq) was going it looked the match was in our hands, but the way we collapsed it's very disappointing.
"We had an opportunity (to win) but we will try not to repeat mistakes in the next two test matches."
The second test begins in Dubai on Saturday.