Angelo Ogbonna scored in the final minute of stoppage time in extra time to help West Ham beat Liverpool 2-1.
Ogbonna headed home from Dimitri Payet’s late free-kick to break the deadlock between the two sides, sending the Hammers through to an FA Cup fifth-round tie with Championship side Blackburn.
The two sides played out a draw over ninety minutes, with Michail Antonio’s strike cancelled out by a clever free-kick from the returning Philippe Coutinho.
But Ogbonna’s header handed West Ham the late victory, meaning that the Hammers have won three and drawn one of their four games against Liverpool this season.
The home fans knew this was likely to be the last FA Cup tie under the lights at Upton Park, and the famous old ground crackled with an atmosphere it will surely be impossible to recreate at the Olympic Stadium.
The match was worthy of the occasion, as well. In a breakneck first half alone, West Ham were twice denied by the woodwork, while Liverpool also rattled a post before Antonio broke the deadlock.
Hammers full-back Joey O’Brien was first to be frustrated by the frame of the goal when his shot was deflected against the base of Simon Mignolet’s upright.
But Liverpool’s rookies, aided by the experience of Coutinho and out-of-favour striker Christian Benteke, looked the more dangerous in the early stages.
The senior duo combined when Benteke reached the byline and cut the ball back for Coutinho, whose first-time shot clipped an upright.
Moments later West Ham playmaker Payet, reportedly the subject of a big-money bid from a Chinese club, curled a superb free-kick against the post.
Antonio headed the rebound back towards goal but Mignolet did brilliantly to scramble back to his feet and make a diving save.
The breakthrough came on the stroke of half-time when Enner Valencia crossed to the far post, where Antonio side-footed a volley into the back of the net.
Yet the lead lasted less than three minutes of the second half, after Liverpool were awarded a free-kick on the edge of the box.
Coutinho shaped to curl the ball over the West Ham wall, but as the players jumped the Brazilian cheekily drove it underneath them and past startled goalkeeper Darren Randolph.
West Ham were convinced they should have had a penalty when Tiago Ilori pulled back Valencia as he attempted to reach Antonio’s cross, but referee Roger East did not agree.
Valencia could have won in normal time but headed Payet’s cross over, while Benteke and Jordon Ibe forced saves from Randolph.
Early in extra time Mignolet was in action again to palm away a curler from Mark Noble before Benteke passed up two clear-cut chances, blazing wide from 16 yards before firing too close to Randolph.
Then England striker Daniel Sturridge, on as a substitute for Liverpool after another spell out injured, fired a rasping drive inches over the crossbar.
But as the clock ticked down Ogbonna trotted forward and managed to get his head to Payet’s free-kick to spark delirious scenes among the Upton Park faithful.