The Liberal Democrats have won the Brecon and Radnorshire by-election, leaving new PM Boris Johnson with a working majority in Parliament of one.
Jane Dodds overturned an 8,038 majority to beat Conservative Chris Davies by 1,425 votes.
Mr Davies stood again after being unseated by a petition following his conviction for a false expenses claim.
It was the first electoral test for Mr Johnson just eight days after becoming prime minister.
It was also the quickest by-election defeat for any new prime minister since World War Two.
Now, with the thinnest majority, he will have to rely heavily on the support of his own MPs and his confidence-and-supply partners the DUP to get any legislation passed in key votes.
It was a bad night for Labour, whose vote share dropped by 12.4% as it was beaten into fourth place by the Brexit Party.
The result means the Lib Dems now have 13 MPs.
Ms Dodds, who is the Welsh Liberal Democrat leader, said: "My very first act as your new MP when I get to Westminster will be to find Mr Boris Johnson, wherever he's hiding, and tell him to stop playing with the future of our community and rule out a no-deal Brexit."
Mr Davies congratulated Ms Dodds saying "I wish her well for the future" and paid tribute to his family saying they had "a difficult time over the past few months".
The turnout was 59.6%, down from 74.6% at the general election, but it is the highest for a by-election since Winchester in 1997.
Neither Plaid Cymru nor the Greens fielded candidates, to try to maximise the Remain vote.
Tory party chairman James Cleverly told BBC Radio 4's Today programme it was a "very close result in a by-election in which the Lib Dems were expected to romp home comfortably".
In a message to Conservative MPs concerned about the government's Brexit policy he said the new prime minister had received a "clear mandate from parliamentarians" and an "even more thumping victory in the leadership election".
"I do think it's incumbent on all Conservatives to support the prime minister in what has been a long-standing Conservative policy," he said.
But recently-elected Liberal Democrat leader Jo Swinson said: "Boris Johnson's shrinking majority makes it clear that he has no mandate to crash us out of the EU."
She denied the party had "played" the system by striking a deal with Plaid Cymru and the Greens.
"I want to have a different voting system but we're working within the system that we have," she said.
Celebrating victory later on Friday morning with party activists, Ms Swinson said the Lib Dems were "winning again" and she would "fight to keep our country in the European Union".
Ms Dodds, 55, lives in the neighbouring mid Wales constituency of Montgomeryshire and is a child-protection social worker.
The Lib Dems have held the rural seat for all but nine of the last 34 years and lost at the 2015 general election.
Plaid Cymru leader Adam Price said the "spirit of co-operation" between the pro-Remain parties had led to Ms Dodds's election, as he called for another EU referendum.
"But if the prime minister is intent on a general election, he should know that Plaid Cymru and the other pro-Remain parties are committed to cooperating so that we beat Brexit once and for all," he said.
Wales Green Party leader Anthony Slaughter said its decision to withdraw from the by-election was "absolutely vindicated" by the result.
"The people of Brecon and Radnorshire have taken the opportunity to cut Boris Johnson's majority in Westminster to a highly unstable one, reducing further the risk of a disastrous crash-out Brexit," he said.
Labour leader Jeremy Corbyn called the result "disappointing".
He added: "The Liberal Democrats won it after doing a deal with Plaid Cymru and the Greens.
"I think that a lot of voters were determined to get rid of the Conservative, and they voted accordingly. So we were squeezed, but it's a place we have not held for a very long time. The area has changed a bit."
Prof Laura McAllister, from Cardiff University's Wales Governance Centre, said the result should not be read as a "resounding victory" for Remain.
She pointed out that the three Brexit-supporting parties had 2-3,000 votes more than the Remain alliance.
But she added: "There are always nuanced undercurrents to this. The reality is Brexit isn't the only issue people were voting on.
"People were probably voting on rural and local issues. We can never categorically say this was about Brexit."
Sir John Curtice, professor of politics at Strathclyde University, said the Conservatives were enjoying a "Boris bounce" and the result was encouraging for the party despite the loss.
But he added: "In an early general election, at the moment at least, the Conservatives would be at risk of losing."
He said the Lib Dems could pick up 40 or 50 seats, which would make winning a large overall majority "rather more difficult for the Conservatives".
By Jonathan Blake, BBC political correspondent
As the ballots were counted, the candidates looked on - neither the Tories nor the Liberal Democrats dared to claim victory or concede defeat.
One thing was certain, the result when it came would be close.
But a win is a win and the Lib Dems will shout this from the rooftops as proof they can cut through with an anti-Brexit message.
The Remain alliance proved to be a winning formula, as Plaid Cymru and the Green Party stood aside to give the Lib Dems a clear run against the Tories.
If the Brexit Party hadn't been standing, the Conservatives might have clinched it.
Labour will look hard at its disastrous result and wonder what might have been with a clearer message on Brexit.
And for the Tories, the people of Brecon and Radnorshire have delivered an unwelcome verdict on their former MP and the new prime minister.
Boris Johnson's hands were already tied in parliament and the ropes around his wrists have just been pulled a little tighter.
At one stage Labour feared it might lose its deposit and blamed voters switching tactically to the Lib Dems.
A Welsh Labour spokesman said: "We always knew this was going to be a difficult night for us, but we're proud of our positive campaign in Brecon and Radnorshire."
"One thing is clear - voters have rejected Boris Johnson and his divisive, out-of-touch UK Tory government."
Political analyst Prof Roger Awan-Scully, from Cardiff University, said: "Labour need to look very closely at this result. Everything points to not just tactical voting for the Lib Dems but also dissatisfaction with Jeremy Corbyn and [First Minister] Mark Drakeford."
The Brexit Party's Des Parkinson, a retired police chief superintendent, who finished third, said: "If you look at the actual total of the vote, the Brexiteers won.
"It shows where the votes are but the prime minister has to deliver a clear Brexit... if he doesn't, then his government is in dire trouble."
The Monster Raving Loony Party pushed the UKIP candidate into sixth place.
Voters have also given their thoughts on the result.
Farmer Trevor Walters voted for Mr Davies and said the Tories might have won, had the Brexit Party not stood, but called the speculation over a Brexit no-deal fallout "scaremongering".
He added: "We're not going to be left in the lurch. I don't think for one second that'll happen. Something will be done to sweeten the blow of all that and get us engaged with a proper trade deal."
Independent book shop owner Emma Corfield-Walters, who backed the Lib Dems, said: "None of us know what's going to happen in the future.
"I think we're all entirely confused on the Brexit issue and I think this result shows us."