Lunch Australia 487 and 186 for 4 (Khawaja 68*, Marsh 42*) lead Pakistan 271 by 402 runs
All eyes after lunch will be on captain Pat Cummins with a declaration likely in the second session.
With the surface deteriorating, proving particularly difficult for batters combating deliveries rearing but also creeping low, Pakistan face a daunting task to avoid a 15th straight Test defeat in Australia.
With Australia resuming at 84 for 2, Khawaja and Steven Smith hoped to build on their 79-run partnership having survived a punishing short-ball assault late on day three.
Shahzad continued his impressive debut after trapping Smith lbw for 45. But Smith immediately reviewed and gestured that he thought the ball was going down the leg side. The DRS upheld the decision by the slimmest of margins with tracking confirming the ball would have clipped the bails.
Smith trudged off the ground desolate as Shahzad claimed his third wicket of the innings having also late on day three roughed up Marnus Labuschagne who had a fitness scare after being hit on his little finger. But a scan cleared Labuschagne of injury.
Travis Head, as expected, counterattacked and started with a fantastic straight drive to the boundary off Afridi. But the cavalier approach proved particularly risky on this surface and against accurate bowling from a Pakistan attack that improved significantly from a disappointing performance earlier in the match.
Head had a couple of close shaves before hitting debutant quick Aamer Jamal straight to cover in a rash stroke.
Jamal, who took six wickets in Australia’s first innings, was on a roll and thought he had Marsh caught behind on the first delivery. But DRS upheld the not out decision with the ball brushing Marsh’s forearm.
The fireworks in the over continued when Jamal, bowling from around the wicket, had Khawaja lbw with a delivery that crept low. But the review showed the impact was just outside the line as Khawaja shouldered on.
Khawaja was soon in agony after copping a short length delivery from Jamal on his arm. It was reminiscent of the blows taken by Labuschagne and Smith, but Khawaja continued after receiving medical attention.
Having made 90 in Australia’s first innings, Marsh batted confidently and took a liking to spinner Salman Ali Agha with a six down the ground. But facing pace bowling continued to be tough with Marsh hit on the helmet by another ferocious short delivery from Jamal.
Marsh continued, but was lucky to still be at the crease when skipper Shan Masood dropped a straightforward chance at mid-off when he was on 23.
Khawaja battled away and reached his half-century in a dogged performance having come into the match under the spotlight after not being allowed to wear shoes which expressed humanitarian views.
Khawaja may look to score quicker after lunch alongside Marsh as Australia ponder a declaration.
Tristan Lavalette is a journalist based in Perth