Israeli Prime Minister Benjamin Netanyahu, who won overwhelming cabinet approval for the lopsided exchange at a special night-time session, has been under constant public pressure to bring Shalit home. He said the soldier would be reunited with his family "in the coming days."
Palestinians in the Gaza Strip greeted the agreement, brokered by Egypt and a German mediator, with celebratory gunfire. Hamas confirmed that it only remained to conclude technical arrangements for the trade.
The breakthrough pact, after many false dawns in years of secret efforts to free Shalit since he was captured in 2006, has no direct bearing on Middle East peace negotiations.
But it was likely to improve the climate for international efforts to restart peace talks. On the sidelines of the agreed swap, the United States said it was hopeful Israel and the Palestinians would hold a preliminary meeting to revive the negotiations on October 23 in Jordan.
Prospects for peacemaking have been clouded by Palestinian President Mahmoud Abbas's request for U.N. recognition of a Palestinian state and by Israeli settlement expansion, which Abbas has said must stop if negotiations are to begin again.
A source involved in the talks said the long-elusive pact had been mediated by Egypt. Its role is likely to strengthen Israel's ties with Cairo, which have suffered since the fall of Egyptian leader Hosni Mubarak in a revolution this year.
Hamas leader Khaled Meshaal said Israel would release 1,027 prisoners in two stages. Within a week, 450 will be swapped for Shalit and the rest will be freed two months later. Twenty-seven women are among those due to be freed.
WHO WILL BE FREED?
It was not immediately clear how many of the prisoners had been jailed for attacks that caused Israeli casualties. Under Israeli law, opponents of their release have at least 48 hours to appeal to courts to keep them behind bars.
Yoram Cohen, head of Israel's Shin Bet internal security service told reporters 110 of the prisoners slated for release in the first stage would return home to the West Bank and East Jerusalem. Another 131 would go to the Gaza Strip, where they reside. Six Israeli Arabs were also on the list.
Cohen said 203 prisoners from the West Bank would be exiled to the Gaza Strip or abroad. The move appeared to be an attempt by Israel to prevent Hamas from regrouping in the territory, controlled by Abbas's Palestinian Authority.
Two of the most famous Palestinian militants serving time in Israeli prisons are not part of the swap, Cohen said.
He said he believed Hamas had softened its demands in the hope of winning favor with Egypt while unrest rocks Syria, a main ally of the group.
Marwan Barghouti, a charismatic leader of Abbas's Fatah faction serving five life prison terms for murder, and Ahmed Saadat, found guilty of ordering the murder of Israel's tourism minister in 2001, will not be freed.
While Shalit's parents have publicly campaigned for his freedom, relatives of Israelis killed in Palestinian attacks have lobbied Netanyahu not to give in to Hamas by releasing prisoners with blood on their hands.
"Murderers are going free and we feel for their victims' families ... but Gilad is coming home," said a woman in the tumult of a crowd who sang and danced at a protest tent that Shalit's family erected months ago near Netanyahu's residence.
There 6,000 Palestinian prisoners in Israeli jails are regarded as heroes of the Palestinian struggle against Israeli occupation and quest for statehood.
"We are happy with this great achievement and we thank our God for that. But our happiness is mixed with sorrow because we were not able to gain the freedom of all prisoners," Meshaal said in a televised address from Damascus.
Israeli television quoted Netanyahu as telling Shalit's parents that, ever since he took office three years ago, "I've been waiting for the chance to make this telephone call" to inform them of the deal.
Shalit's family had accused him of not doing enough to secure his release.
Announcing to his cabinet, and television cameras, that a deal had been signed earlier in the day, Netanyahu said he feared time was running out for Shalit amid political upheaval in the Arab world.
"I believe we have reached the best agreement possible at this time, when storms are raging in the Middle East. I don't know if we could have reached a better agreement, or even achieved one at all, in the near future," he said.
"It's possible that this window of opportunity would have closed for good and we never would have brought Gilad home."
In Gaza the families of men jailed for life by Israel waited to see if their names would be on the list.
Israel has carried out several lopsided prisoner swaps in the past, notably in 1985 when hundreds of Palestinian prisoners were freed in exchange for several soldiers captured by a guerrilla group in Lebanon.
The ordeal of Shalit, a fresh-faced corporal, transfixed Israel after the tank gunner was captured by militants who tunneled their way out of Gaza and then forced him back over the border.
He was 19 at the time and had begun his mandatory three-year army service nearly a year earlier.
Shalit, who also holds French citizenship, was last seen in a videotape released by his captors in September 2009, looking pale and thin.
He received no visits from the International Committee of the Red Cross, despite many appeals.