By Martin Patience
BBC News, Jerusalem
For months, members of Fatah and Hamas held behind-the-scenes negotiations with the Army of Islam, the Palestinian group that was holding the BBC reporter. Those negotiating attempts ultimately failed.
Alan Johnston in the moments after his release
In June, in a bloody power struggle between the Palestinian factions, Hamas forces routed Fatah forces in the Gaza Strip.
The territory effectively became Hamas-controlled and the militant Islamist movement promised to restore law and order there. They said freeing Alan Johnston was a top priority.
More than two weeks ago, Hamas gunmen began surrounding the Sabra district of Gaza City where Alan Johnston was held.
Hamas gunmen also killed a member of the Dogmush family - some of whose members have close connections with the Army of Islam - and arrested several other figures from the group, including its spokesman.
The Army of Islam then kidnapped nine Hamas-affiliated students.
It is not clear whether any deal was struck between Hamas and the Army of Islam, but late on Tuesday night a chain of events was set in motion.
Here are the dramatic events of the last few hours of Alan Johnston's captivity.
TUESDAY 3 JULY
2000 (1700 GMT): Hundreds of Hamas gunmen flood into the Sabra district of Gaza City - controlled by the Dogmush family - bolstering other Hamas forces already present in the area.
They close off streets and alleyways, take positions on rooftops, and begin to move closer to buildings where they believed Alan Johnston was being held.
2300: As the siege continues, senior Hamas officials say they have heard from members of the Army of Islam that they are willing to enter serious negotiations to free Alan Johnston.
WEDNESDAY 4 JULY
0030 (2130 GMT): The leader of the Hamas military wing, Ahmad Jabari, meets the leader of the Army of Islam, Mumtaz Dogmush, to discuss the release.
The meeting is held in an eastern part of Gaza City, away from the area of the ongoing siege.
0045: Nine students kidnapped by the Army of Islam are set free in an apparent gesture of goodwill.
0145: Ahmad Jabari and Mumtaz Dogmush reach a provisional agreement that Alan Johnston and all other prisoners should be released.
They call for an Islamic religious leader to preside over the agreement.
0230: The religious leader meets both men and issues a decree saying that kidnap is un-Islamic.
0245: The two men agree on the release of Alan Johnston and all other prisoners.
0300: Four Army of Islam members are brought to the meeting place and subsequently released.
0305: A convoy of Hamas vehicles is sent into the Sabra district to collect Alan Johnston. Initially, they are not allowed in as many Hamas gunmen stationed there have not heard about the deal.
0330: Alan Johnston is brought to the meeting place where he is handed over to Hamas officials accompanied by a Palestinian BBC colleague.
0400: Johnston is taken to the house of the former Palestinian Prime Minister and Hamas leader Ismail Haniya.
0430: Johnston holds a news conference with Mr Haniya and various other senior Hamas officials at the former PM's office. They eat a traditional Palestinian breakfast.
0730: UK diplomats and BBC officials meet Alan Johnston at Mr Haniya's office.
0745: Johnston leaves in a convoy for the Erez crossing into Israel.
0800 (0500 GMT): Johnston leaves Gaza.