Yorkshire need nine wickets on the final day to win only their second Roses match in 25 years after setting Lancashire an improbable target of 349 at Headingley.
Having built on their first-innings lead to reach 77-3 second time around overnight, that rapidly became 89-6.
But, led by Liam Plunkett (57) and Adil Rashid (34), they rallied to post 236.
The County Championship leaders then reached 41-1, for the loss of Tom Smith, when bad light ended play early.
Yorkshire’s first home Roses match victory since 2001 was starting to look less likely early on in the day after Adam Lyth (48) had his leg-stump uprooted by Wagner, nightwatchman Steve Patterson edged Smith to stand-in wicketkeeper Steven Croft and home skipper Andrew Gale was caught at short leg.
But, not for the first time, the Tykes were rescued by their tail, first-innings half-centurion Rashid putting on 41 with Tim Bresnan before adding a further 74 under the floodlights with Plunkett.
Lancashire were handicapped by the loss of paceman Tom Bailey, who injured his side in the first innings, but Wagner came up with his second four-wicket haul of the match.
The South African’s 4-71, on top of his first-innings 4-75, takes him to 28 wickets this season, to join Lancashire team-mate Kyle Jarvis as Division One’s leading wicket-takers.
In the Red Rose’s second innings, Smith drove Patterson gently to Plunkett at short cover, while Luke Procter kept Haseeb Hameed company until they went off with 16.4 overs remaining.
Yorkshire coach Jason Gillespie confirmed after the close of play that Australia coach Darren Lehmann had visited his home for a chat over the weekend – but that he had not been offered any job with Cricket Australia and would not be putting in any application for any post.
Yorkshire coach Jason Gillespie:
“Rashid, Bresnan and Plunkett got us out of a very tight spot.
“Lancashire are a good competitive side and it has been a tough battle. But that is what you expect from a Roses clash. It is a good challenge for us.”
Lancashire paceman Neil Wagner:
“We had to fight very hard in tough conditions but we are still in with a chance if we can do the hard work.
“We knew we had to put everything into the morning session and that the ball would move around under the floodlights, but Yorkshire batted very well and all credit to them.
“I was pretty chuffed to take eight wickets in the game – or I will be if we go on and win it. I like to bowl aggressively and do it for long spells, not tail off after five or 10 overs.”