City enjoyed a regal dominance up until Ilkay Gundogan's 26th-minute opener but Jeffrey Schlupp levelled six minutes later before Townsend turned the contest on its head with an outstanding long-range volley.
Luka Milivojevic increased City's misery and delight at Anfield with a 51st-minute penalty that means Liverpool will celebrate Christmas with a four-point lead at the Premier League summit.
Kevin De Bruyne's overhit cross sailed in five minutes from time but Pep Guardiola's side were unable to undo the damage in the late rally that followed.
Gundogan was involved alongside Bernardo Silva in carving through Palace for Leroy Sane to shoot wastefully into the side-netting in the 10th minute.
The Germany midfielder showed his countryman how it was done when he eased into the box to head home Fabian Delph's searching cross.
Schlupp allowed Gundogan to run off him for the opener but more than atoned by shifting outside a tired Kyle Walker challenge and arrowing a strike into the bottom right corner.
If that finish was impressive, Townsend’s remarkable goal 10 minutes before half-time was something else altogether as he put his left foot sweetly through an audacious volley, leaving Ederson with no chance.
Pep Guardiola introduced City's record goalscorer Sergio Aguero for centre-back Nicolas Otamendi early in the second half but the reshuffle led to confused defending as Townsend headed Aaron Wan-Bissaka's cross against the post.
Walker made a dreadfully rash challenge as Max Meyer sought to retrieve the rebound to concede a penalty and Milivojevic, who had a penalty saved by Ederson in a 0-0 draw at Selhurst Park last season, made no mistake.
City's response was disjointed before Sane cracked the outside of the right post with a 77th-minute free-kick.
De Bruyne's freak goal then set nerves jangling and Aymeric Laporte and Gabriel Jesus spurned free-headed chances for Palace to hold on.
What does it mean? Advantage Liverpool
There is still a long way to go in the title race but City now have two defeats to set against Liverpool's unbeaten record. Back-to-back away games are on the horizon and they cannot afford to lose any more ground before hosting Jurgen Klopp's men in a blockbuster clash on January 3. The "can City be stopped?" fatalism of a couple of weeks ago now feels preposterous. Talking of fatalism, many Palace fans would have dismissed the notion of sitting six points clear of the drop zone at Christmas.
Combative and composed Wan-Bissaka stars for Palace
Townsend will rightfully hog the headlines but Wan-Bissaka did much to enhance his burgeoning reputation, keeping the in-form Sane on a short leash and making a succession of brave challenges. The 21-year-old's teasing cross also paved the way for Palace's game-sealing third goal.
Landmark 2018 takes toll on ragged Walker
A key figure in City's record-breaking Premier League campaign last season and England's run to the World Cup semi-finals, Walker looked to be running on empty as he was at fault for the two avoidable Palace goals. Danilo is back to fitness as an unused substitute and it feels like time for Guardiola to take an often excellent performer out of the firing line.
Key Opta Facts
- City have lost two of their last three Premier League games (W1) – as many as in their previous 61 combined.
- Palace registered their first away league win at City since December 1990, having drawn three and lost nine since then before today.
2010 - Roy Hodgson is the first English manager to win a Premier League game at the Etihad since Harry Redknapp in May 2010 (P39 W0 D3 L36, before today). Jungle. pic.twitter.com/Txlw2eKNVz— OptaJoe (@OptaJoe) December 22, 2018
- This was City's first home Premier League defeat against a non-big six side since February 2016, when they lost 1-3 to Leicester City under Manuel Pellegrini.
- Townsend has scored 17 Premier League goals – with 10 of those coming from outside the box.
- Since his Premier League debut, Milivojevic has scored 13 penalties - five more than any other player in that time.
Guardiola's men travel to Leicester City on Boxing Day, a week on from winning a penalty shoot-out in the EFL Cup quarter-final at the King Power Stadium. Palace return to Selhurst Park for a potentially pivotal clash against Cardiff City.