Brendan Rodgers’ side were in danger of suffering the latest setback of a disappointing campaign after Mark Noble’s penalty and a Steven Gerrard own goal put the Hammers 2-1 up following former West Ham player Glen Johnson’s early opener.
But, to their credit, Liverpool dug deep to equalise through another Hammers old boy Joe Cole before West Ham defender James Collins turned a Jordan Henderson cross into his own net with 11 minutes remaining.
Liverpool’s second successive Premier League win lifted them up to 10th place and remarkably they are now within four points of the top four despite their early-season struggles.
The result was all the more impressive as they achieved it without suspended star striker Luis Suarez.
“It was a brilliant win for us. West Ham have been terrific this year but I thought our team were outstanding,” Rodgers said.
“We’ve threatened to score three, four and five goals through most of this season, but through not being ruthless or having bad luck we haven’t scored as many as we would have liked.”
Hammers boss Sam Allardyce added: “You are never in control of a game completely against a Liverpool side. We knew it would be difficult.
“We lost (Mohamed) Diame with a hamstring injury which didn’t help and then we lost some of our forward momentum.”
Rodgers’ team took the lead in the 11th minute when Gerrard played the ball out to Johnson on the touchline and the England right-back cut inside Matt Jarvis with ease before arrowing a strike into the top corner of Jussi Jaaskelainen’s goal.
Johnson did not celebrate out of respect for his former club, but Liverpool should have been further ahead soon after.
Raheem Sterling had two chances to double Liverpool’s advantage, the teenager first firing wide of Jaaskelainen’s near post and then sending a low drive just off target.
But, after a tepid start, West Ham started to threaten and they equalised in the 36th minute.
A set-piece was cleared as far as Guy Demel whose shot hit the arm of Joe Allen as the Liverpool midfielder closed him down.
Referee Lee Probert deemed the block to be intentional and pointed to the spot, with Noble stepping up to convert the penalty.
It got worse for Liverpool two minutes before half-time when they switched off after conceding a free-kick which Noble took quickly to find Jarvis in space on the left.
Jarvis sent over a cross and Gerrard rose to nod clear, but instead powered his header past the stunned Jose Reina and into his own net.
Sterling came close to equalising after the break as his effort from the edge of the area forced Jaaskelainen to flick the ball over his bar.
Rodgers sent on Henderson for Brazilian midfielder Lucas in the 71st minute and the change sparked Liverpool’s late revival.
It was Cole, who has endured a miserable time with Liverpool since joining from Chelsea three years ago, who equalised in the 76th minute when he took Sterling’s pass and curled a superb shot past Jaaskelainen.
And Liverpool completed their great escape three minutes later when Henderson whipped a low cross towards Shelvey and Collins got there first to accidently deflect the ball over Jaaskelainen into his own net.