In a much-anticipated prisoner exchange that could have broad implications, Israel and Hamas said an Israeli soldier abducted to Gaza five years ago would be swapped for around 1,000 Palestinians accused of militant activity.
Israel's government approved the deal following a three-hour debate after both Israeli prime minister Benjamin Netanyahu and Hamas leader Khaled Mashaal announced the agreement in televised comments.
Mr Netanyahu said the captured soldier, Sgt Gilad Schalit, would return home within days. Mr Mashaal, portraying the agreement as a victory, said the Palestinian prisoners would be freed in two stages over two months.
Hamas and Israel are bitter enemies. Hamas has sent dozens of suicide bombers into Israel, killing hundreds, and Israel blockaded Gaza after Hamas seized power there in 2007, carrying out a large-scale invasion in 2009 to try to stop daily rocket attacks on Israel. More than 1,500 Gaza Palestinians have been killed in Israeli raids and airstrikes since the soldier was captured.
In the northern Gaza town of Jebaliya, thousands of Hamas supporters flocked the streets, led by masked militants. Cars with loudspeakers played praise for Hamas. Thousands of other Gazans rushed to their border with Egypt, clutching Palestinian and Egyptian flags, tossing flowers and cheering.
Gaza's Hamas prime minister Ismail Haniyeh smiled as he threw sweets to celebrating backers.
The deal maintains a decades-long tradition of lopsided exchanges that have come under increasing criticism in Israel - and ends a period of tortured indecision by Israeli governments torn between securing the release of a single soldier and the risk that freed militants might return to violence that could cost many more lives.
Mr Mashaal said 1,000 men and 27 women prisoners would be released, the first 450 over the next week and the rest within two months. He said they would include 315 prisoners serving life sentences, suggesting they were convicted of attacks that caused the deaths of Israelis.
Seeming to confirm Israel's fears, Mr Mashaal said that those who are released "will return to ... the national struggle".
Sgt Schalit was captured in a cross-border raid in June 2006 by Palestinian militants who burrowed into Israel and dragged him into Gaza after killing two other soldiers.