Boris Johnson has been left with a working majority of just one in the House of Commons after the Tories were defeated in the Brecon and Radnorshire by-election.
The Liberal Democrats won the mid-Wales constituency by a margin of 1,425, swinging the vote nearly 12% from the Tories.
An electoral pact saw Plaid Cymru and the Green Party stand aside to boost the chances of a pro-EU MP being elected, and Lib Dem leader Jo Swinson predicted further alliances in future.
The first major electoral test of Mr Johnson as Tory leader became his first defeat with the announcement of Jane Dodds’ victory in the early hours of Friday.
The Tory loss will add to the PM’s challenges to steer Brexit through Parliament and may increase the chances of a snap general election.
With the vote coming eight days after Mr Johnson was appointed, it is the quickest by-election defeat for any PM of the post-War period.
In a celebratory appearance alongside her new MP, Ms Swinson said: “This by-election victory shows that the people of Brecon and Radnorshire not only have a first class MP in Jane Dodds but they have shown the people of Britain that we can do better than the choice on offer between Boris Johnson and Jeremy Corbyn.”
She added: “I will fight to keep our country in the European Union, and we now have in Parliament one more MP who will fight to make that happen.”
The Lib Dem leader said she had exchanged messages with the Green and Plaid leaderships and told the BBC: “I think there will be more co-operation in future elections.”
Welsh Lib Dem leader Ms Dodds said: “My very first act as your MP when I arrive in Westminster will be to find Mr Boris Johnson wherever he is hiding and tell him loud and clear: ‘Stop playing with the future of our communities and rule out a no-deal Brexit now.’”
She pushed into second place the Conservative candidate Chris Davies, who was the constituency’s MP until he was ousted in a recall petition after he admitted submitting false invoices for expenses.
Despite 19% of the electorate petitioning for his recall, the Tories selected Mr Davies to fight to reclaim the seat.
The Lib Dems secured 43% of the vote, the Tories 39% and the Brexit Party 10%.
Fourth-placed Labour narrowly avoided losing its deposit by securing just over 5% of the share.
The vote may also be seen as an early victory for the “Remain alliance” of anti-Brexit parties, with Plaid Cymru and the Greens agreeing not to field candidates in order not to split the pro-EU vote.
Some Conservative commentators blamed the Brexit Party for splitting the “pro-Brexit vote” – as together their vote share amounted to 49%.
Tory chairman James Cleverly said it was “disappointing to lose a parliamentary colleague” but, attempting to put a brave face on the defeat, he told BBC Radio 4’s Today: “What we saw was a very close result in a by-election in which the Lib Dems were expected to romp home comfortably.”
Plaid Cymru’s Westminster leader Liz Saville Roberts said: “The victory of Remain cross-party co-operation and Jane Dodds in Brecon and Radnorshire last night will give those in Downing Street plotting a snap general election pause for thought, because it shows they cannot take the public for granted.”
Mr Davies won the seat from the Lib Dems in 2015 and secured a majority of 8,038 in the 2017 general election.
But this was overhauled with a swing to the Lib Dems of 11.96%.
The loss leaves the PM with the support of 319 MPs, including the DUP which props up his majority, while opposition parties now have 318.
Mr Davies’ recall came after he pleaded guilty in March to submitting two false expenses invoices for nine photographs costing £700 to decorate his new office.
He was fined £1,500, ordered to pay £2,500 towards legal costs and told to carry out 50 hours of community service.
In his losing speech, Mr Davies thanked his team and family “who have had a difficult time over the last few months”.