The result in a seat that Labour has dominated for nearly a century represents an embarrassing setback for the prime minister.