(CNN) -- After months of psychological torment by guards loyal to Libyan leader Moammar Gadhafi, when a commotion arose outside the door of his solitary confinement cell in Tripoli's most notorious prison, Matthew VanDyke was sure he was going to be executed.
But what happened next is sure to be the Baltimore-based adventure writer's wildest chapter yet.
Reached by phone in a safe house in Tripoli, VanDyke, 32, described a harrowing 30 hours that took him from Abu Salim prison to the safe house under the protection of anti-Gadhafi rebels.
The freelance journalist and aspiring travel writer had not been heard from since mid-March, shortly before he was arrested in the city of Brega by forces loyal to Gadhafi. He had been without contact with any family member, government official or aid organization since March 12, when he last spoke to his mother.
Early Wednesday in Tripoli, he said, he heard prisoners yelling and shouting and banging, not unlike the sounds he had heard as guards rousted out other prisoners during his six months of solitary confinement. As he stared at the white walls where he had ticked off the days until he lost track, he thought, "This is it, they've come to lynch me."
A hand opened the small slit in the door where his food was passed through. He heard someone angrily shouting and pointing at verses in an open Quran pushed through the door.
Not long after, someone hammered off the lock on the door.
At first he thought it was a trap, he told his mother by phone, maybe a ruse to get him in greater danger. But then the realization dawned: He had a chance to escape.
"I saw some other prisoners and they were cheering and pointing down the hall," said an exhausted VanDyke. "I knew I was free, but didn't know what to do or what was going on."
VanDyke had no possessions to gather up, only a wet and ragged prison uniform. He found a pair of sandals left behind by another fleeing prisoner and then escaped the prison along with hundreds of other inmates.
Many of those held in the facility were political prisoners, or had been caught up in crackdowns on rebel sympathizers or on the many battlefields edging closer to the capital.
VanDyke joined up with one small group walking through the Abu Salim neighborhood of Tripoli, not knowing that they were in one of the last areas still controlled by Gadhafi loyalists. They could hear explosions and gunfire, but also got news that rebels were taking control of the city.
The group made their way to a mosque. There, the imam and other neighborhood leaders were handing out cash, trying to help people desperately short of supplies amid the chaos and electrical blackout in the area.
VanDyke said he moved on, to a makeshift hospital where he got some help and directions to a local wealthy person who had access to a working phone.
It was there he was able to make his first phone call back to his worried girlfriend, Lauren Fischer, and mother, Sharon VanDyke, back in Baltimore.
"I've always known he was OK," Sharon VanDyke told CNN late Wednesday.
The retired schoolteacher described the relief and surprise she felt after watching CNN endlessly for days, hoping for news and praying for her son's safety. Now she and Fischer are focused on getting him out of Libya.
VanDyke said he was eventually able to hook up with another escaped inmate who had ties to rebel forces and could speak English. The man gained access to a car and two guards armed with AK-47 assault rifles, and they sped through the night past rebel checkpoints.
Eventually he arrived at a house once occupied by an American expatriate couple who had hastily fled in the earliest days of the conflict.
VanDyke said the home still had food on the counters, remnants from the last-second escape of the last people there, a welcome windfall for him after months of rancid prison food. And the working phone there was a lifeline to the outside, he added.
The writer is one of at least four American civilians who have been freed from Libyan prisons, the U.S. government said Wednesday.
"We can confirm that all U.S. citizens who were known to be detained in Libya have been released," U.S. State
Department spokesperson Victoria Nuland said in a prepared statement:
"The families of those detained individuals have been notified of their freedom and welfare. We remain concerned about the welfare and safety of those U.S. citizens who remain in Tripoli. For safety and privacy reasons we are unable to provide additional details."
VanDyke was surprised to hear that other Americans had been held in the prison and was unaware of their escape or current situations.
Having traveled to nearly a dozen Middle Eastern and African countries since 2007, VanDyke left for Benghazi in late February. Friends there had told him what was happening in Libya during the early stages of the revolution.
"He had a great respect for the people and the country, not the politics," his mother said.
VanDyke's loved ones aren't certain why he was arrested, although he had a U.S. passport and press credentials when he was seized on a day trip to Brega.
By phone, the exhausted writer said he would have a fuller account to tell of his capture after he was able to sleep and recuperate, but he said his arrest had been a surprise and he did not know the reason for it.
VanDyke had been confined in two prisons, including Abu Salim, from which he and the hundreds of others escaped Wednesday, one day after rebels seized Gadhafi's compound in the city. Libyan officials had recently acknowledged VanDyke was in custody.
His mother traveled to Turkey in May to seek assistance in his release, but the country had just closed its embassy in Tripoli.
His family, which has been assisted by the State Department and U.S. Rep. C.A. "Dutch" Ruppersberger of Maryland, hopes to see VanDyke in several days.
"He said he was in good physical health," said his mother. "He was sorry we did not know for sure he was OK."
Sharon VanDyke said her son was unaware that al Qaeda leader Osama bin Laden had been killed by U.S. commandos in May.
She and Fischer, 28, expect VanDyke, who first went to Libya in 2008, to complete a book about the people he has met and places he has seen in Mauritania, Iraq, Turkey, Afghanistan, Egypt and other countries. He toured several of those nations by motorcycle.
"Despite everything, I have no problems with Libyans," VanDyke said, describing his safety and plans in Tripoli. "But I'll be determined to stay here to see the end of Gadhafi."