England looked set to lose their first qualifying game since 2009 when Milivoje Novakovic put Slovenia ahead, but Jack Wilshere scored two fine goals to put the visitors in front.
Substitute Nejc Pecnik thought he had grabbed a historic equaliser with six minutes to go, but less than two minutes later, Rooney capitalised on a defensive error and slotted past Samir Handanovic to score his 48th international goal.
The England captain is now level with Gary Lineker and one shy of Charlton’s 49-goal record.
It was a famous evening too for Roy Hodgson, who became the first England manager to guide his team through an unbeaten season since Graham Taylor did so in the 1990-91 season.
Hodgson will also take pride in his team’s performance. It was the complete antithesis of the one they put on in the drab goalless draw in Dublin last week.
Rooney will grab the headlines because he scored the winner, but Wilshere’s first two goals for his country were absolute stunners.
Despite missing chances, Raheem Sterling looked threatening and Jordan Henderson played surprisingly well at right-back in the second half.
A year on from England’s World Cup defeat against Italy in Manaus, they have ended the season with a comprehensive lead at the top of Group E after six matches.
Hodgson warned on the eve of the match that his players would face a stern test of their mental strength, and he was right. The atmosphere inside the stadium could not have been more different than the one they experienced in Dublin.
Behind the home goal, bare-chested supporters behind a “Ljubljana Ultras” flag shook the foundations of the Stadion Stozice. Hodgson’s team also had a different look to it, with Fabian Delph, Andros Townsend and Kieran Gibbs brought into the starting XI.
Hodgson stuck with Sterling, but his faith in the Liverpool forward initially looked misplaced.
The controversial 20-year-old found space in the box, but lifted the ball over the home goal.
Sterling spurned another chance, cutting in from the right and firing wide because of a deflection.
Rooney then drew a top-class save from Handanovic. England were dominating, but they lacked the killer touch Hodgson had urged them to find.
England were made to pay for their profligacy when they went 1-0 down in the 38th minute.
Phil Jones set the ball rolling with a truly awful throw-in, which went straight to a Slovenia player inside his own box. Slovenia shifted the ball up the pitch with alarming pace. Two moves later, Josip Ilicic had sent Novakovic clear. Gary Cahill failed to play Novakovic offside and he slotted the ball past the on-rushing Joe Hart.
England could not believe it.
Everyone was left scratching their heads when Hodgson shifted Henderson to right-back following the introduction of Adam Lallana for Jones.
In fairness to Hodgson, it proved to be a wise move. Lallana twisted and turned past two defenders in the box. When another got his foot to the ball, it went straight to Wilshere and he banged the ball into the top right-hand corner.
England pushed for a second straight away, but again lacked a cutting edge.
Rooney missed two good chances but England were ahead again in the 73rd minute when Wilshere scored his second.
Henderson picked the ball up off Delph and sped down the right before cutting the ball back to Lallana. He flicked the ball into Wilshere, who took one touch before rifling a left-foot shot into the far corner. England fans rose to their feet to applaud.
Eleven minutes later, they were stunned as Slovenia equalised. Pecnik, a former flop at Sheffield Wednesday, headed home Bojan Jokic’s cross after losing Gibbs at the back post.
England kept their composure though and blew the candles out on the home celebrations when Jokic slid in on substitute Theo Walcott and Rooney slotted home.
Hodgson saluted Rooney and Wilshere after they fired England to a thrilling win.
“Wayne Rooney’s performance says a lot of things about him as a man,” the England manager said of his captain.
“Just before he took that chance he took a nasty blow from an elbow, which could have decked many a player and led to him losing his discipline. It didn’t and he got up.
“He had one or two chances that had gone begging, but when the ball fell to him he still stuck it away with aplomb.
“We can rely on that man. I’d have liked him to get a hat-trick today so all the talk of the record would be finished, but he has plenty of time.”
Rooney, who ended the season with 22 goals, said: ‘’It’s great to score the winner and it was a great team performance.”
Rooney took his strike well, but the best goals of the evening belonged to Wilshere.
It has taken five years for Wilshere to break his duck with England, but on this evidence it was worth the wait.
“His whole performance was quality,” Hodgson said of the Arsenal midfielder.
When asked if it was the best goal he had seen in his three-year tenure as England boss, Hodgson said: “It’s always the last one which excites me the most. I’d have to have all the goals put before me (to make a decision).
“I remember Andy Carroll’s bullet header (in Euro 2012), but I’m very happy with Jack, the goals he’s scored and his performance.
“In the second half, he was controlling that midfield, it was a performance we’ve seen from him for a while now.”
Slovenia coach Srecko Katanec accused his team of schoolboy defending after the defeat, which puts a dent in their hopes of finishing second in Group E.