After years of negotiations that went nowhere, Israeli Prime Minister Benjamin Netanyahu and the militant Islamist group Hamas announced Tuesday that Staff Sgt. Gilad Shalit, an Israeli soldier held captive in the Gaza Strip for more than five years, would be released.

Gilad Shalit in an undated photo. (AP)

Although Shalit was captured only in 2006, the story of his imprisonment is a long and convoluted one, involving many failed arbitrations, diplomatic efforts and rescue attempts, as well as a divide in the Israeli public over how to secure his release.

The story begins in Nahariya, Israel, where Shalit was born in 1986 to French-born parents, making him both an Israeli and French national.

But it really only gets started when Shalit began military service in the Israel Defense Forces in 2005, where, despite medical issues, he chose to serve in a combat unit.

Just a year after he joined, Palestinian militants tunneled into Israel and attacked an Israeli army post where Shalit was serving. The young soldier was injured by a grenade and then captured by the militants.

It made him the first Israeli soldier captured by Palestinians since 1994.

The day after his capture, Shalit’s captors — which included the Izz ad-Din al-Qassam Brigades, Popular Resistance Committees and Army of Islam — issued a statement that said Shalit would be released if Israel agreed to release all female Palestinian prisoners and all Palestinian prisoners under the age of 18.

Israel declined, saying it would not negotiate to release a prisoner.

In the months that followed, Hamas issued an ultimatum for Shalit’s release, and Israel made a rescue attempt. Diplomatic efforts followed, involving Egypt, which attempted to mediate, and France, because of Shalit’s citizenship.

As the years went by, Shalit’s family relentlessly campaigned for his release, begging for international intervention and saying his confinement was contrary to international humanitarian law.

Hamas did not allow Shalit to be visited by the Red Cross or seen, except in a video released by the militant group in 2009 in which he appeared alive and well.

But Shalit’s captors continued to ask for a deal in which a large number of Palestinians prisoners jailed in Israel be released in exchange for Shalit, to which Israel refused.

Although part of the Israeli public thought the deal was a good idea, another part, including Netanyahu, said that if Shalit was released on Hamas’s conditions, it would free dangerous militants and put Israel’s security at risk.

Without a deal, Hamas refused to budge.

And then, on Tuesday, it was quite suddenly reported that Israel and Hamas reached an agreement for Shalit’s release.

In exchange for the release, Hamas has said that about 1,000 Palestinian prisoners will be released, including 315 serving life sentences and 34 of the longest-serving prisoners. All 27 Palestinian women held in Israeli jails will also be freed.

Netanyahu’s cabinet overwhelmingly approved of this deal, which was signed in Cairo by both sides.

How had such a remarkable turnaround happened?

The Post’s Joel Greenberg reports that the Shalit family’s relentless campaign to free their son had eventually “won the hearts of the Israeli public,” until “Netanyahu ultimately bowed.”

The deal also gives Netanyahu a boost at home at a time when he has been accused of leaving his country isolated amid widespread change in the region.

In his remarks, Netanyahu hinted at that, saying:

I believe that we reached the best agreement that could be achieved at this time, when storms are buffeting the Middle East

I don’t know whether the near future would have enabled us to achieve a better agreement, or an agreement at all, and it is very likely that this window of opportunity created by current circumstances would have closed for good, and we would not have brought Gilad back at all.