Joanna Lumley, David Cameron and Nick Clegg react to the vote
Gordon Brown's government has suffered a shock defeat in the Commons on its policy of restricting the right of many former Gurkhas to settle in the UK.
MPs voted by 267 to 246 for a Lib Dem motion offering all Gurkhas equal right of residence, with the Tories and 27 Labour rebels backing it.
Lib Dem leader Nick Clegg called the government's position "shameful".
Immigration minister Phil Woolas told MPs new proposals would be published before Parliament's summer recess.
In a statement, he said: "This government respects the will of the House of Commons."
He added that all outstanding applications for UK residence by Gurkhas would be dealt with by the end of May.
Mr Brown's first significant defeat as prime minister came despite last-minute concessions being offered to rebel Labour MPs.
The Commons vote is not binding, but it represents an embarrassment for the government.
It comes at a time when Mr Brown is facing criticism over other issues, including his reform plans for MPs' expenses, which will also go to a vote on Thursday.
It is the biggest Commons victory achieved by the Liberal Democrats since their formation two decades ago, and is the first time a government has lost an opposition day debate since James Callaghan in January 1978.
The real worry for Gordon Brown and his whips is that this could merely be the warm-up for Thursday's vote on expenses
There were shouts of "resign" as the numbers were announced. The Lib Dems said 28 Labour MPs had voted for their motion - although that is thought to include one Labour MP who voted both for and against the motion.
After the vote Mr Clegg and Mr Cameron joined actress Joanna Lumley, who has been campaigning on the issue, and Gurkhas outside Parliament.
Mr Clegg said: "This is an immense victory on a series of fronts: for the rights of Gurkhas who have been waiting so long for justice, a victory for Parliament, a victory for decency."
He added that it was "the kind of thing people want this country to do".
Mr Cameron said it was "embarrassing" for the prime minister because his efforts to strike a "shoddy deal" with Labour rebels had failed.
He added: "Today is a historic day where Parliament took the right decision. The government have got to come back with immediate proposals so that the Gurkhas can have an answer."
MPs reject Gordon Brown's Gurkha policy
Among Labour MPs voting for the Liberal Democrat motion were home affairs committee chairman Keith Vaz, ministerial aide Stephen Pound and former cabinet minister Andrew Smith.
Mr Pound said he had resigned as a parliamentary private secretary to vote against the government.
Labour MP Martin Salter, chairman of the Parliamentary group on Gurkhas' rights, abstained.
He told the BBC he refused to support the government but wanted to acknowledge the concessions made.
He added: "It is the amount of abstainers that did it. Comparatively few Labour MPs actually voted for the Lib Dem motion but an awful lot of people sat on their hands as a way of showing their determination to finish this issue."
Some 36,000 former Gurkhas have been denied UK residency because they served in the British army before 1997.
Ministers had introduced new rules allowing more soldiers to settle here based on long service, medals received, and those injured in battle.
Mr Cameron and Mr Clegg said the policy should be changed quickly
The Home Office said that new rules would allow about 4,300 more to settle, but the Gurkha Justice Campaign said it would be just 100.
Defending the policy at prime minister's questions earlier, Gordon Brown said: "Since 1997 we have taken the first action to give justice to the Gurkhas.
"During that period of time the first ever Gurkhas to have rights of settlement in Britain has been agreed and 6,000 have now applied successfully and come into the country."
He said they had created equal pay and pensions for the Gurkhas and doubled the pensions of people staying in Nepal.
But he said: "We have got to balance our responsibilities to those who have served our country with the finance that we need to be able to meet these obligations - and not base our offer on money we cannot afford."
Ms Lumley said the campaigners were "elated" as they had expected to lose the vote.
"When it came through we saw it on the screen and I can't tell you the sense of elation," she said.
This page is best viewed in an up-to-date web browser with style sheets (CSS) enabled. While you will be able to view the content of this page in your current browser, you will not be able to get the full visual experience. Please consider upgrading your browser software or enabling style sheets (CSS) if you are able to do so.