Israel and Hamas have agreed a landmark deal to secure the release of captured Israeli soldier Gilad Shalit in exchange for over a thousand Palestinian detainees, officials on both sides said.
If the accord is implemented, it will end an ordeal that has lasted more than five years for the young soldier, who has become a national icon in Israel since his capture by Gaza-based militants in June 2006.
It will also be a major political coup for Gaza's Hamas rulers.
The Israeli government early Wednesday formally approved the deal, which came out of the blue, with Prime Minister Benjamin Netanyahu announcing the details in a live address shortly after presenting it to his government during an emergency session on Tuesday night.
"We have concluded arduous negotiations with Hamas to release Gilad Shalit. He will be coming home in the next few days," he said.
"I believe we reached the best possible agreement at this time," Netanyahu added.
"It is very possible that this window of opportunity that has been created at this time... would have closed once and for all, and we wouldn't have been able to bring Gilad back at all."
The agreement was backed by 26 ministers after a five-hour meeting while three nationalist Cabinet heavyweights -- Foreign Minister Avigdor Lieberman, Infrastructure Minister Uzi Landau and Strategic Affairs Minister Moshe Yaalon -- voted against it.
Israel's top officials in charge of security, including Defence Minister Ehud Barak, chief of staff Benny Gantz and Shin Beth and Mossad (internal and external security forces) bosses Yoram Cohen and Tamir Pardo all backed the deal with the Islamist Hamas, which rules the Gaza Strip.
Hamas chief Khaled Meshaal confirmed the deal in his own televised speech in Damascus, saying the agreement, which was mediated by Egypt, would see more than 1,000 Palestinian detainees freed in exchange for Shalit.
"Hamas and Israel have reached an agreement under which 1,027 Palestinians, of whom 27 are women, will be freed in two phases," he said at a news conference broadcast on Arabic-language news channels.
He said the first phase of the deal would see 450 prisoners freed "in one week," with another 550 Palestinians to be freed "in two months".
However, a top Israeli intelligence official ruled out the release of two key prisoners, telling reporters that influential Palestinian leader Marwan Barghuti and top PFLP militant Ahmed Saadat were not on the list of those to be released.
Signup to our newsletter and follow us on Facebook and Twitter!
His remarks contradicted an earlier report from a senior Palestinian official who told AFP the two were among those slated to be freed.
In Israel, the news brought excited crowds to the protest tent set up by Shalit's parents outside Netanyahu's Jerusalem home.
Noam and Aviva Shalit could be seen smiling and laughing as well-wishers crowded into the makeshift structure emblazoned with messages of support.
As supporters offered their congratulations, the couple smiled but tried to reserve judgment, waiting for official confirmation that their son was on his way home before allowing themselves to express their joy.
"I'll celebrate when I have reason to be happy," Noam Shalit told AFP.
Palestinian president Mahmud Abbas "warmly" welcomed the deal, negotiator Saeb Erakat told AFP in a phone call from Venezuela, describing it as a "Palestinian national achievement".
And in the Gaza Strip, Hamas said "tens of thousands" of Palestinians had flooded streets in the northern part of the territory to celebrate the expected release of Palestinian detainees.
"Crowds of tens of thousands are heading towards the Khulafa Mosque in Jabaliya camp at the beginning of the human floods of joy for the victory of the resistance and the completion of the prisoner exchange," Hamas said.
French President Nicolas Sarkozy was "delighted" at the news of the accord, his office said.
Sarkozy spoke with Noam Shalit, the father of the 25-year-old who also has French nationality, and told him of his "joy that Gilad will soon be released," the Elysee Palace said in a statement.
The French head of state had also spoken by phone with Netanyahu and congratulated him for this "major success", the Elysee said.
The deal, which was reached on Thursday and signed on Tuesday, comes after years of attempts to agree a prisoner exchange which failed despite the efforts of Egyptian and German mediators.
Shalit was captured in a deadly cross-border raid on June 25, 2006 by militants from three Gaza-based groups including Hamas, the Popular Resistance Committees and a Salafi group called the Army of Islam.
The deal has repeatedly stalled over the issue of which prisoners would be released and where they would be allowed to go, with Israel insisting that some go to Gaza or be exiled overseas, and not return to their homes in the West Bank.
It was not immediately clear how the two sides had resolved that issue.