
After letting slip an early lead, the London club were under the cosh for much of the second half but a huge piece of luck turned the game their way.
Carlton Cole showed excellent agility as he headed home George McCartney's cross after just eight minutes but Kenwyne Jones equalized with a header of his own seven minutes before the break.
Sunderland continued to improve after their equaliser and it took a brilliant save from Richard Green to stop them taking the lead as he tipped a shot from Grant Leadbitter onto the post.
The visitors looked the team most likely to win it but West Ham then took the lead in the most fortuitous of circumstances 12 minutes from the end.
Substitute Luis Boa Morte fired a low cross from the left, which was missed by the Sunderland defence.
The ball ran to another substitute, Nolberto Solano, and his shot hit the foot of the post, only to rebound and cannon off the leg of goalkeeper Craig Gordon, who had been beaten by Solano's shot.
Sunderland tried to force a second equaliser but West Ham completed victory when Boa Morte fed Craig Bellamy, whose neat finish was too good for Gordon.
West Ham are now 10th, ahead of Everton on goal difference.