The victory saw United stretch their lead at the top of the table to eight points ahead of Arsenal's match against Blackburn in Saturday's late kick-off.
West Ham had looked to be on course for a deserved victory after two early penalties from Mark Noble fired the Londoners into a 2-0 lead at Upton Park against a woeful United side.
But Rooney spearheaded a dramatic United transformation in the second half, curling in a superb free-kick on 65 minutes to throw the leaders a lifeline before levelling with a rasping low shot eight minutes later.
The comeback was complete on 78 minutes when fullback Fabio Da Silva charged down the right flank and whipped a ball back into the box.
The Brazilian's pull back grazed West Ham defender Matthew Upson on the arm and referee Lee Mason had no hesitation in pointing to the spot.
Rooney duly stepped up to slam in the penalty before substitute Javier Hernandez completed the scoring with a close range effort on 84 minutes.
Defeat was desperately hard on the Hammers, who had protested angrily after Mason failed to send off United captain Nemanja Vidic.
The Serbian defender was lucky to stay on the pitch after hauling down Demba Ba shortly before half-time, receiving only a yellow card instead.
A few minutes into the second half Vidic again breathed a sigh of relief after escaping a second yellow for a clumsy tackle from behind on Ba.
The defeat halted West Ham's recent revival and saw them slip into the relegation zone once again on goal difference.
Short link: