Prime Minister Benjamin Netanyahu said the deal had been reached after arduous talks, and that Sgt Shalit would be back with his family “within days”.
Israel is likely to release hundreds of Palestinian prisoners in exchange.
Sgt Shalit was captured in a cross-border raid in 2006, and repeated attempts to free him have failed.
But Mr Netanyahu said in a national TV address: “We have concluded arduous negotiations with Hamas to release Gilad Shalit. He will be coming home in the next few days.”
The militant group Hamas, which runs Gaza, said in a statement that there were tens of thousands of people on the streets celebrating the deal.
Almost every family in Israel has a son or daughter serving in the military. Shalit’s capture resonates with them all. He is probably the most recognisable figure in the country.
It is for that reason that the government is ready to swap him for – according to Hamas officials -1,000 Palestinians held in Israeli jails.
Hamas has been on the back foot in recent months after Palestinian leader Mahmoud Abbas’s attempt to get Palestinian membership at the UN gained huge support in the occupied territories.
Hamas leader Khaled Meshaal appeared on TV to hail the swap deal as a victory for the Palestinian people.
He promised to carry on working to free every Palestinian prisoner held in Israeli jails.