West Ham s Jarrod Bowen celebrates scoring his equaliser against Chelsea . AFP
Thomas Tuchel's side went ahead through Thiago Silva but Manuel Lanzini's penalty drew West Ham level.
Mason Mount restored Chelsea's advantage just before half-time, only for Jarrod Bowen to blast West Ham's second equaliser soon after the interval.
Chelsea dominated for long periods but made too many mistakes at both ends and paid the price with three minutes left as Masuaku's miscued cross somehow found its way past Edouard Mendy.
It was the Blues' first defeat in their last 13 games in all competitions and their spell at the top of the Premier League might be over by Saturday evening.
They are just one point ahead of second placed Manchester City, who visit Watford later on Saturday, with third placed Liverpool also able to go above Chelsea if they win at Wolves.
Fourth placed West Ham once again showed their ability to make life uncomfortable for the big guns.
The Hammers had already ended Liverpool's long unbeaten run this season, as well as knocking Manchester City and Manchester United out of the League Cup.
Now Chelsea have joined the list of star-studded teams driven to distraction by Moyes' well-drilled side.
After admitting Chelsea rode their luck to beat Watford in midweek, Tuchel will be concerned at another unusually disjointed display from the European champions.
Reece James has been in superb form of late and Chelsea's right wing-back tested Lukasz Fabianski with a 20-yard drive before providing the cross that Kai Havertz headed straight at the Hammers keeper.
- Chelsea collapse -
Chelsea's early dominance was rewarded in the 29th minute when Silva met Mason Mount's corner with a downwards header that Fabianski could only push into the net via the far post.
But their concentration wavered and West Ham were gifted an equaliser five minutes before half-time.
Jorginho played a suicidal backpass that Mendy scrambled to reach before Bowen.
As the Senegal keeper tried to dribble around Bowen, he lost control of the ball and sent the West Ham forward crashing to the turf with a panicked tackle.
It was a comedy of errors and Lanzini delivered the punchline as he drove the penalty into the roof of the net.
Jorginho's howler came just a week after his woeful control allowed Jadon Sancho to score in the draw with Manchester United.
Chelsea's response was instant as a sweeping move put them back in front four minutes later.
Hakim Ziyech's majestic long pass picked out Mount, who ran in behind Ben Johnson to caress a sublime volley past Fabianski from just inside the area.
An injury to Havertz on the stroke of half-time meant Romelu Lukaku was introduced after the interval, but the Belgian was anonymous as he makes a slow recovery from an ankle injury.
Heading into the weekend, Chelsea had conceded just six league goals this season -- the lowest amount of any team in the top five European leagues.
But, missing the injured N'Golo Kante as the midfield shield in front of them, there was a lack of cohesion at the back for the Blues and Moyes' men took advantage to snatch a 56th minute equaliser.
Vladimir Coufal was able to poke a pass to Bowen on the edge of the area as Chelsea fatally backed off and the forward drilled a superb low strike through Andreas Christensen's legs and into the far corner.
It was the first time Chelsea had conceded more than one goal in a Premier League game since May and there was worse to come in the 87th minute.
When Masuaku took possession and shaped to cross, there seemed little danger for Chelsea, but the defender's delivery suddenly swerved in mid-air, deceiving Mendy as it arched past the stunned keeper.
(For more sports news and updates, follow Ahram Online Sports on Twitter at @AO_Sports and on Facebook at AhramOnlineSports.)