What could prove a historic deal to release hundreds of Palestinian prisoners in exchange for an Israeli soldier held captive in Gaza has been agreed between Israel and Hamas, concluding five years of negotiations that have repeatedly collapsed in acrimony and violence.

The Israeli Prime Minister, Benjamin Netanyahu, said that he had telephoned the parents of Gilad Shalit, the soldier captured by Hamas in a cross-border raid in 2006, to tell them that their son would be home "within days".

Mr Netanyahu said that a deal with Hamas was initialled last Thursday and signed yesterday, adding that he got "the best deal we could get". He had earlier been quoted by an Israeli television station as saying "the window has been opened for a historic deal".

Hamas leader Khalad Meshal confirmed the deal and said that it would see 1,000 Palestinian prisoners released in two stages, the first 450 within a week. The group would include 315 serving life sentences and all Palestinian women in Israeli prisons, he said, hailing the deal as a "national accomplishment" and declaring those to be released were heroes who would "come back to freedom and struggle against occupation".

A delegation led by a top Hamas official, Mahmoud Zahar, had arrived in Cairo on Monday night from the group's headquarters in Syria. A source involved in the talks said the deal had been mediated by Egypt.

There were conflicting reports last night over whether Marwan Barghouti, a former top local commander of Fatah, would be part of the deal. If so, it would be a hugely significant step as the release of Barghouti, the most prominent imprisoned Palestinian who is serving multiple life terms for his role in deadly attacks against Israelis, has long been a sticking point in negotiations.

German and Egyptian mediators have tried to broker deals between Israel and Hamas over the past five years. Those talks have repeatedly broken down, and Israel has carried out a number of military strikes and imposed a blockade on the Gaza Strip, partly to pressure Hamas over the case of the soldier. In 2009, it was reported that an exchange was close on similar terms to those which have now reportedly been agreed.

The deal collapsed, however, after disagreements over the release of a number of "heavyweight" prisoners held by Israel, who were accused of planning terrorist attacks on the country. Other sticking points have been whether prisoners would be allowed to return to the West Bank and Gaza Strip, or be sent into exile.

If agreed, the deal would see the release of Sergeant Shalit, 25, after five years in captivity in the Gaza Strip.

He was 19 when he was captured in June 2006 by Palestinian militants who burrowed into Israel and took him into Gaza. His prolonged captivity has been an emotive issue in Israel, where military service is mandatory for Jewish citizens, and many Israelis have identified with the Shalit family's predicament.

Key captives...

Gilad Shalit

The Israeli and French national was a 19-year-old sergeant in the Israeli army when he was captured by Palestinian militants and taken to Gaza in 2006. Since his capture in a cross-border raid, the only contact between Shalit and the outside world has been three letters, an audio tape and a DVD video.

Marwan Barghouti

Even though he is a long-time Fatah leader, Barghouti is seen as a possible unifying figure among Palestinians. Despite being imprisoned by Israel in 2002 for five counts of murder, he is considered a strong candidate to succeed Mahmoud Abbas as President of the Palestinian Authority.

Richard Hall

Israel's long fight: How prisoners triggered a campaign

With conscription a fact of life in Israel and with a perception that the country is surrounded by enemies fuelling a strong sense of the need for national solidarity, the government in Jerusalem always faces a difficult calculus when determining how far to go to bring back a captured soldier.

That Israel is prepared to go to extreme lengths to bring back its young conscripts alive is indicated by the numbers of Palestinian prisoners that Benjamin Netanyahu was prepared to release for Gilad Shalit. But if Mr Netanyahu and his cabinet were seen to be reluctant to make the necessary sacrifices for the soldier's freedom, the political costs could be significant.

In the past, limits on Israel's willingness to bargain have been defined by whether those up for exchange have "blood on their hands". The steps soldiers are instructed to take to prevent a snatch are also unusual. Under the so-called "Hannibal Directive", it is said that Israeli forces would be ordered to fire upon anyone abducting one of their soldiers even at the cost of the life of the victim.

A soldier's death can be borne, the reasoning goes, but his lengthy incarceration can be disastrous for national morale.