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 historic evening for Roy Hodgson too, 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 to the one that 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 once 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.