Boris Johnson’s Conservative party has lost one of its safest seats in England in an extraordinary by-election victory for the Liberal Democrats.

Sarah Green, the Lib Dem candidate, secured 21,517 votes in Chesham and Amersham, giving her a solid majority of 8,028 over Tory candidate Peter Fleet with 13,489 votes.

Sir Ed Davey, the leader of the Lib Dems, said the victory showed that the so-called blue wall of Tory seats in southern England was more fragile than people thought.

The contest in the ruling Conservative party’s former stronghold in an affluent part of Buckinghamshire was prompted by the death of Cheryl Gillan, a former cabinet minister who held the seat with a majority of 16,223.

The result came as the Conservatives sit at about 44 per cent in the national polls — boosted by a successful Covid-19 vaccination campaign — and the Lib Dems at about 7 per cent.

But there were stirrings of change in May’s local elections, when the Lib Dems went from zero seats in Amersham to controlling the town council and made gains elsewhere in southern England.

The Lib Dem victory will cause nervousness within the Tory party that its heavy emphasis on “levelling up” — channelling more funding to low-income northern seats — is prompting resentment in some parts of the south.

The bookmakers odds on a Lib Dem victory were about 10:1 before voters went to the polls. The prime minister, speaking on Friday, described the result as “disappointing” but said it was “bizarre” to suggest the loss was part of a wider trend of Tory losses in the south.

The Tories have made gains in the Midlands and northern England, with their backing for Brexit and promise to redistribute money away from the south-east helping them take scores of former Labour party seats in the red wall.

One Lib Dem aide said: “I do think that the Conservatives are going to have to think very carefully about how they continue to ride these two horses of both the red wall and the blue wall.”

Southern Conservatives are concerned that Johnson’s planning reforms will allow the Lib Dems to set themselves up as protectors of the greenbelt. One seasoned Tory activist described the result as “no surprise”, adding that “planning is going to screw us”.

The Lib Dem aide said local voters had seen the heavy media coverage of the recent Hartlepool by-election, where the Conservatives seized one of the most deprived towns in England from Labour. “They noticed that hardly anyone was covering Amersham, because no one ever thought it could change hands. That taking voters for granted was a massive mistake,” the person said.

Davey made 16 visits to the south Buckinghamshire seat during the campaign, focusing on issues including planning. Many voters in southern England are unhappy with government plans to force through the development of large numbers of new houses through new legislation.

There is also widespread local opposition to HS2, the new rail line that passes through the constituency. Both the Conservatives and Lib Dems support the project.

The Tories sent senior figures including Johnson, chancellor Rishi Sunak and party co-chair Amanda Milling to the constituency to campaign in recent days.

The morale-boosting result for a party that has struggled in recent years, represents a 25 per cent swing away from the Tories and takes the Lib Dems to 12 seats in the House of Commons.

The Lib Dems reached an electoral high in the 2005 election with 62 seats. In the 2010 general election they returned 57 MPs and became the minority partner in a Conservative-led coalition government for five years. This damaged their popularity and subsequently reduced them to a small rump of MPs.

The Tory campaign was described as “remarkably bad” by one party activist in the constituency. “Not just complacent and far too slow to spot the threat,” the person said.

The Green candidate Carolyne Culver received 1,480 votes. Natasa Pantelic of Labour got 622, down from the party’s 7,166 votes in 2019, suggesting that there was a large degree of tactical voting. The turnout was 52 per cent.