Spinner Rashid Khan claimed five wickets to put Afghanistan in sight of their maiden Test victory after bowling out Ireland for 288 on day three of the one-off game on Sunday.
Chasing 147 to win, Afghanistan were 29 for one at stumps. Ihsanullah Janat, on 16, and Rahmat Shah, on 11, were batting at close of play in Dehradun.
Spinner Andy McBrine had opener Mohammad Shahzad caught behind for two before Janat and Shah, who made 98 in Afghanistan's first innings total of 314, played out the last few overs of the day.
Ireland batsmen Andy Balbirnie and Kevin O'Brien had earlier hit half-centuries before Khan returned career-best figures of 5-82 to bowl out Ireland in the final session of play.
"Feeling happy to get some wickets for the team when the team needed me," Khan, who has taken seven wickets in the match, told reporters.
"It was tough to grip the new ball because I had a finger injury and tough for me to pitch it on the right length as was paining a lot," he said, referring to the injured middle finger of his bowling arm.
Balbirnie made 82 and O'Brien 56 before a 10th-wicket 58-run partnership between James Cameron-Dow and Tim Murtagh set up a potentially tricky chase for the Afghans.
Cameron-Dow was left unbeaten on 32 after number 11 Murtagh, who hit 54 not out in Ireland's first innings score of 172, was the last man out on 27.
"I am not surprised, if you have seen Test cricket even tailenders have records," said Khan.
"Plan was to bowl as many good deliveries as possible. But they chose the right deliveries to score and that's how they made runs."
'Ebbed and flowed'
Earlier Balbirnie put on 104 runs for the third wicket with James McCollum, who made 39, to frustrate the Afghanistan bowlers in the first session.
An umpiring howler cost Ireland their first wicket of the day with Paul Stirling adjudged lbw off paceman Yamin Ahmadzai for 14 after the opener got a thick edge onto his pad.
Balbirnie, who resumed the day on 14, stood firm to register his maiden Test half-century with a boundary off Mohammad Nabi.
Ireland took lunch at 124-2 but a batting collapse in the second session saw them slip to 157-6, with Khan and Salamkheil sharing the spoils.
Balbirnie was caught behind off Salamkheil after his 149-ball stay at the wicket, and Khan trapped McCollum lbw in the very next over as Afghanistan regained control.
"It ebbed and flowed. We had a good first session and we could have batted for a good part of the day and build a substantial lead," said Balbirnie.
"So from my point of view it was really frustrating, because I was a set batsman and it was a lapse of concentration (my dismissal). But the 10th wicket partnership gives us a fight."
O'Brien, who took Ireland past their 142-run deficit and into a lead, then put on 63 runs with George Dockrell, who made 25, to take the attack to the opposition bowlers.
But Khan got the two dangermen back in the pavilion after tea to get into the Ireland tail that once again wagged.
The left-handed batting duo of Cameron-Dow and Murtagh put on another defiant stand for Ireland, who had posted an 87-run last-wicket partnership in their first innings.
Both sides are playing their second Test after making their debuts in the five-day format last year.
Afghanistan lost to India inside two days in Bangalore while Ireland went down to Pakistan in Dublin.