The Hammers had taken the lead inside two minutes with a strike from Robbie Keane but Villa wrestled back control with Darren Bent’s header shortly before the interval in the Premier League clash at Upton Park.
West Ham goalkeeper Robert Green pulled off a string of top-class saves to keep his side in the game but Villa snatched a deserved victory with a header from substitute Agbonlahor in added time.
Villa’s victory moved them up to ninth in the table on 40 points, seven clear of the drop zone, while Avram Grant’s struggling side slipped deeper into trouble.
The Hammers have five games left to save their season. The next two are away at Chelsea and Manchester City.
West Ham had billed the game as a celebration of the “Best of British” in anticipation of St George’s Day and the Royal Wedding and there were 14 players from these shores in the two starting line-ups.
Scott Parker was missing for West Ham with a troublesome Achilles and replaced in midfield by Gary O’Neil while Carlton Cole, Victor Obinna and Lars Jacobsen all came into the team beaten 3-0 by Bolton last weekend.
Aston Villa featured two changes from their victory over Newcastle, with Emile Heskey and former Hammers captain Nigel Reo-Coker replacing Agbonlahor and Jean Makoun.
West Ham had promised a “traditional street party atmosphere” and the Upton Park faithful were waving their claret and blue pennants inside two minutes after Keane opened the scoring.
O’Neil’s blocked shot from a West Ham corner fell to Thomas Hitzlsperger, who lofted a header towards Keane on the edge of the six yard box.
Keane was played on-side by Ashley Young, who had been marking the far post, and the on-loan Tottenham striker turned to rifle his shot past Brad Friedel.
Keane was heavily involved in a bright West Ham start. He turned James Collins and played in Cole but the England striker was denied by Luke Young’s block.
Stiliyan Petrov’s interception sparked another Villa attack and Heskey had Bent in support but he was closed down by the West Ham defence.
In an action-packed game, West Ham then had strong penalty appeals turned down after Cole was bundled over by Richard Dunne as he chased Keane’s long ball.
Dunne, the last man back for Villa, made contact just outside the area as Cole was primed to shoot. Referee Mark Halsey looked towards his assistant for guidance but gave nothing.
West Ham kept coming forward and Cole forced a smart near-post save from Friedel after he had been fed by a long pass from O’Neil.
Villa thought they had equalised after 23 minutes with a Bent header but it was disallowed by referee Halsey for a push on Jacobsen.
Villa began to enjoy a period of possession but West Ham stayed patient in defence, allowing the visitors to move the ball around without any real penetration.
But all that work was undone when Mark Noble was caught in possession inside the West Ham box and Villa pounced.
Heskey robbed Noble of possession and Luke Young curled in the cross for Bent, who got in front of Wayne Bridge and flashed his header past Green.
Bent’s goal sparked a key shift in momentum. There was no sense of a street party inside Upton Park now, just nervous tension as Villa started the second period with a new lease of life.
The visitors were only denied the lead by two magnificent saves in quick succession from Green in the West Ham goal.
Upson’s fluffed header allowed Bent to steal in but his low strike from a tight angle was blocked by Green’s one-handed save and the ball shepherded out for a corner.
Cole cleared Collins’ powerful header off the line before Noble conceded a careless free-kick on the edge of his own box.
Ashley Young curled his shot around the West Ham wall but Green was quick to get across and turn the ball behind with a flying finger-tip save.
Grant, who had been forced to replace Manuel Da Costa with Danny Gabbidon in the first half, sent on Zavon Hines for Obinna and then Ba for the injured O’Neil.
Villa remained the more dangerous side. Bent scuffed a golden chance to win the game after being picked out in the box by Walker.
West Ham could not create anything meaningful. Hines saw a snapshot blocked by Dunne and Hitzlsperger skied his follow-up effort.
And in the first minute of injury time, Villa finally got their just desserts with Ashley Young finding space down the left and picking out substitute Agbonlahor to head home the winner.