A crowd of 2,000 Hammers supporters saw their side take a deserved first-half lead through Tomas Soucek at the London Stadium.
But United kicked off a huge week with a much-needed win as three goals in 13 minutes from Paul Pogba, Mason Greenwood and Marcus Rashford moved Ole Gunnar Solskjaer's men up to fourth and within two points of the Premier League leaders.
West Ham were left to rue not making more of a host of first-half chances before Solskjaer turned to his bench to dig United out of a hole.
The Red Devils need a point when they travel to RB Leipzig on Tuesday to secure a place in the Champions League last 16 before hosting Manchester City next weekend.
However, Solskjaer's plan to give Bruno Fernandes and Rashford a rest before the trip to Germany backfired as United were lucky to stay in the game before the break.
Jarrod Bowen had a goal ruled out for offside before he teed up Pablo Fornals to head a great chance into the side-netting.
Fornals came even closer moments later when the Spaniard stabbed a shot off the post.
But the goal West Ham deserved came seven minutes before the break when Soucek turned home Declan Rice's flick-on from a corner at the back post.
Sebastien Haller should have doubled the lead for David Moyes's men, but the Ivorian slipped with the goal at his mercy after rounding Dean Henderson, who was deputising for the injured David de Gea.
Fernandes and Rashford were summoned at the break and United were a different side in the second-half as for the fifth consecutive away Premier League game they came from behind to win.
Pogba produced the flash of brilliance he is capable of but has so rarely shown in a United shirt to kickstart the comeback as he curled home a long-range shot from Fernandes's pass.
Moyes was furious on the touchline as he believed the ball had gone out of play from Henderson's punt into Fernandes's path, but VAR could find no conclusive proof to disallow the goal.
Three minutes later, the visitors lead as Alex Telles's cross was brilliantly controlled and thrashed into the net by Greenwood for his first Premier League goal of the season.
United were now in full flow as they could counter-attack into acres of space behind the Hammers' defence.
Rashford had a sighter as he struck the post from a brilliant Fernandes through ball.
But the England international was clinical with a second chance as this time Mata unlocked the West Ham defence and Rashford coolly lifted the ball over the advancing Lukasz Fabianski 12 minutes from time.