At tea, Pakistan were 179-5 — chasing 319 to win — with Sarfaraz and Saud Shakeel fighting to salvage a draw on 65 and 27 respectively after New Zealand dominated the first session.
A series-deciding defeat was on the cards at lunch with the total on 125-5, but Sarfaraz and Shakeel carried the fight to the New Zealand bowlers with a sixth-wicket stand that has so far yielded 99 runs.
Sarfaraz, who has hit six boundaries, completed his fourth consecutive Test half-century — and 22nd overall — sweeping a four off spinner Ish Sodhi.
The best winning chase in a Test in Pakistan was 314 by the home team against Australia in Karachi in 1994.
Sodhi (2-49) and fellow spinner Michael Bracewell (2-33) jolted Pakistan earlier with three wickets in the first session.
Resuming two down without any runs on the board, Pakistan lost Imam-ul-Haq (12), skipper Babar Azam (27) and Shan Masood (35) before reaching three figures.
Haq fell to a rash drive off Sodhi as the ball spun in and crashed onto the stumps.
Azam and Masood then put on 42 runs and were looking solid when Bracewell came into the act.
He first removed Azam with a sharp turning delivery that was edged to wicketkeeper Tom Latham, while Masood miscued a lofted shot and was caught brilliantly by Kane Williamson at mid-off.