After United's bus was pelted with missiles en route to the ground, they came from behind to lead 2-1 as Anthony Martial's brace negated Diafra Sakho's 10th-minute opener for West Ham.
But Michail Antonio equalised before centre-back Winston Reid smuggled a header past visiting goalkeeper David de Gea in the 80th minute to bring the 2,398th and final game at West Ham's Boleyn Ground home to a crashing crescendo.
United manager Louis van Gaal said that the pre-match scenes had affected his younger players, saying: "It's not the first time that it happens, but I have a long experience in football. There are players who don't have experience.
"It shall have an influence, but I don't think we have to look for excuses."
The result left United in fifth place in the Premier League and means that they can only pip Manchester City to fourth place if they win at home to Bournemouth on Sunday and City lose at Swansea City.
That unlikely scenario confronts the under-pressure Van Gaal with the unwanted consolation of a Europa League berth, although his side also face Crystal Palace in the FA Cup final.
West Ham manager Slaven Bilic was left to celebrate a first league win over United in 15 attempts, which took West Ham back above Southampton and into a Europa League slot.
"We didn't stop believing," Bilic told Sky Sports before dashing off for an on-pitch ceremony involving several former club greats.
"We totally deserved it. It was a great night. It is history, not just a game. It is against them, fighting for Europe, late-night game. All the boxes you need for a special type of game was tonight."
But while the manner of victory was a fitting send-off for the 112-year-old ground, ahead of West Ham's move to the Olympic Stadium, it was marred by pre-match scenes that saw the United bus struck with cans and bottles.
It resulted in a 45-minute delay to kick-off, and there were further scenes of disorder when De Gea had bottles thrown at him following Martial's equaliser.
The Football Association subsequently announced an investigation. Martial's return, after missing Saturday's 1-0 win at Norwich City with a hamstring problem, boosted United, but they barely had time to draw breath as West Ham surged ahead.
Aaron Cresswell's pass down the inside-left channel found Manuel Lanzini running in behind Juan Mata and from his pull-back, Sakho's shot clipped Daley Blind and found the bottom-left corner.
After Angelo Ogbonna and Dimitri Payet had threatened, Andy Carroll spurned a gilt-edged chance when, having been sent clean through by Payet, he shot straight at De Gea.
Moments later Antonio headed in a cross from Mark Noble, only for his celebration to be cut short by a linesman's flag indicating that the ball had already gone out for a corner.
Lanzini drilled wide, Payet shanked over after Noble had robbed Martial and when the visitors finally created a chance, Wayne Rooney crossing dangerously towards Marcus Rashford, Ogbonna intervened.
Seeking greater control in midfield, Van Gaal introduced Michael Carrick for Morgan Schneiderlin at the interval and within five minutes his team were level. De Gea's goal-kick reached Rashford, whose pass found the overlapping Mata, and the Spaniard darted past Ogbonna before squaring for Martial to tap in his 16th goal of the season.
After Sakho had squandered a great chance for West Ham by turning Payet's free-kick over the bar, Martial put United in front by skinning Reid and dinking a shot inside Darren Randolph's near post.
But Upton Park's final chapter was not yet written and after Payet had picked out Antonio to send a bullet header past De Gea, the Frenchman crossed for Reid to notch a winning goal that left the venerable rafters quivering.