It seemed England would have a commanding first innings lead as Australia collapsed to 117 for nine in reply to the Ashes-holders' first innings 215.
But 19-year-old Agar went on to make the highest score by any Test No 11, surpassing Tino Best's 95 for the West Indies against England at Edgbaston last year.
Together with fellow left-hander Phil Hughes (81 not out), Agar shared a 10th wicket Test record partnership of 163 to take Australia to 280.
That topped the previous last wicket record stand of 151 held jointly by Brian Hastings and Richard Collinge for New Zealand against Pakistan at Auckland in 1972/73 and Azhar Mahmood and Mushtaq Ahmed for Pakistan against South Africa at Rawalpindi in 1997/98.
Left-arm paceman Mitchell Starc then followed up by taking two wickets in successive balls to leave England floundering at 11 for two.
But England captain Alastair Cook (37 not out) and Kevin Pietersen (35 not out) rebuilt the innings with an unbroken stand of 69 that left the hosts 80 for two at stumps -- a lead of 15 runs.
"When I walked to the crease I was thinking we were in a little bit of trouble but the wicket was good," Agar told BBC Radio's Test Match Special after stumps.
"I have always tried to play freely and naturally and I don't really get too nervous. There are a lot of people who would love to be doing what I'm doing."
Australia resumed Thursday on 75 for four, a deficit of 140 runs, after an overcast first day where seamer Peter Siddle rocked England with five for 50.
But Thursday saw sunshine and blue skies which promised better batting conditions for Steven Smith, 38 not out overnight, and Hughes.
James Anderson, who on Wednesday had gone past England fast bowling great Fred Trueman's record of 307 Test wickets, made the breakthrough Thursday on his way to an innings return of five wickets fir
Smith, driving loosely at Anderson, gaining reverse-swing, was caught behind by wicket-keeper Matt Prior for 53.
This was the start of an Australia collapse that saw five wickets lost for nine runs.
Agar did survive a desperately close stumping appeal from Prior off spinner Graeme Swann on six, with Australia then 131 for nine -- 84 runs adrift -- as third umpire Marais Erasmus eventually ruled in his favour.
Agar, a shock selection for this Test after playing just 10 first-class matches for Western Australia with a highest score of 71 not out, then cashed in with shots reminiscent of India's Yuvraj Singh including two magnificent straight sixes off Swann.
But, in sight of a century, Agar -- recently playing club cricket for southern English side Henley -- pulled Stuart Broad to Swann at deep midwicket to end a two-and-quarter hour innings of 101 balls including 12 fours and two sixes.
"I tried to hit that last ball a little flatter, I was a little disappointed not to get a hundred but I didn't think I would get 98 when I walked out there so I can't complain too much," Agar said.
England then saw Joe Root caught down the legside by wicketkeeper Brad Haddin off Starc and next ball Jonathan Trott was given not out by Pakistani umpire Aleem Dar after a raucous lbw appeal.
Australia reviewed the decision and, with the technology at his disposal not detecting an edge, Erasmus instructed the decision be reversed.
Tea was then taken with England 11 for two, a deficit of 54 runs, and Starc on a hat-trick.
But Pietersen survived the hat-trick immediately after the resumption as Starc bowled a wayward delivery.
Cook and Pietersen carefully repaired the damage.
Agar, in his primary role as a left-arm spinner, saw Pietersen, on 25, edge him into Haddin's pads.
But next ball Pietersen cover-drove Agar, originally added to the squad as a 'development' player for just the first two Tests, to the boundary as the bowler found wickets harder to come by than runs.