Peter Crouch put Stoke ahead when Jonny Evans’ clearance hit him and flew in.
Robin van Persie levelled from close range but Marko Arnautovic’s curling free-kick restored Stoke’s lead.
United were facing a second home defeat of the season but with 12 minutes left Rooney nodded home a corner and moments later Hernandez grabbed the winner.
Relief was the over-riding emotion around Old Trafford at the final whistle following a game in which David Moyes’ side laboured for long periods but finished with a flourish to end Stoke’s resistance and give their manager a badly needed win.
It was the sort of fightback that was a trademark of Moyes’ predecessor Sir Alex Ferguson, who was watching from the stands.
But, in truth, United still appeared a shadow of the side they were under their old boss and Stoke could have been out of sight by half-time.
With only four goals from their first eight games, the Potters were the lowest scorers in the top-flight before kick-off but their manager, former United striker Mark Hughes, still described his old side as “vulnerable” this week.
He was proved right after just four minutes. Erik Pieters broke clear down the left and, although home keeper David De Gea kept out Crouch’s first-time shot with an instinctive stop, the ball flew into the United net after Evans smashed it against the Stoke striker.
United’s response saw them dominate possession but their threat was restricted by a lack of width or imagination in their passing.
Stoke, in contrast, looked dangerous whenever they broke forward. Crouch went close to making it 2-0 when he hooked a close-range volley over the bar and Steven N’Zonzi saw his snap-shot effort brilliantly kept out by De Gea.
At the other end, Potters keeper Asmir Begovic was only forced into action on the half-hour mark when he tipped over Rooney’s 25-yard shot.
United’s equaliser came out of nothing after 43 minutes when Begovic kept out Rooney’s header but could not react in time to stop Van Persie following up.
Nevertheless, Stoke still deservedly went in ahead at half-time after Phil Jones needlessly barged into Arnautovic on the edge of the area. The Austrian got up to guide a superb free-kick into the top corner for his first Premier League goal.
The home fans expected a response after the interval but they did not get it until Adnan Januzaj was introduced just before the hour mark.
Until then, United had been pedestrian in their passing and had barely threatened Begovic’s goal. Afterwards, they began to advance down the flanks and deliver dangerous crosses.
With the pressure building, Rooney met Van Persie’s corner with a looping near-post header that dropped into the opposite corner of the net.
And Hernandez, who like Januzaj had started the game on the bench, got the winner two minutes later, timing his run perfectly to convert Patrice Evra’s precise cross from the left.