Hichilema, whose trial was due to begin in the morning, had been in custody since April over allegedly failing to give way to President Edgar.
Hichilema, whose trial was due to begin in the morning, had been in custody since April over allegedly failing to give way to President Edgar Lungu's motorcade.
After his release, he described his detention as "brutal" and vowed to use his imprisonment to bolster the opposition United Party for National Development (UPND) that he leads.
"This is where we commence our walk to true freedom," he told cheering supporters. "I want to assure you that we are stronger now than before. We are 10 times stronger now than before."
Lillian Kayuni, director of public prosecution, earlier told the court in Lusaka that she would be "entering a nolle prosequi", or formal notice of abandonment.
Hichilema was driven away from jail, standing up in the sun roof of his vehicle and waving to waiting crowds.
He spoke later at the UPND headquarters, flanked by his wife Mutinta and the five aides who were arrested with him.
All six denied the treason charges at a plea hearing on Monday, and police officers had again sealed off the court precinct for the start of the scheduled trial on Wednesday.
Zambia has enjoyed relative stability since its first multi-party election in 1991.
But last year's election was marked by clashes between supporters of Lungu's Patriotic Front (PF) and the UPND.
Lungu, who narrowly beat Hichilema in the election, has dismissed allegations of growing authoritarianism and has accused his rival of trying to overturn the election result.
Hichilema, 55, claims the election was rigged and has refused to recognise Lungu as the country's president.
In June, parliament suspended 48 UPND lawmakers for boycotting an address by Lungu three months earlier.
And last month, the president invoked a state of emergency, increasing police powers of arrest and detention after a series of arson attacks blamed on opposition parties.
The government has also increased pressure on media outlets that support the opposition, eroding Zambia's reputation as a stable democracy.
Hichilema was arrested after he allegedly put Lungu's life in danger when his convoy failed to make way for the presidential motorcade in a high-speed road drama caught on video camera.
The two men were both travelling to Western Province for a traditional ceremony.
Days later, more than 100 armed police surrounded Hichilema's house outside Lusaka, firing tear gas before detaining him and his aides.
The treason charges were dropped after Commonwealth secretary-general Patricia Scotland held talks with Lungu and Hichilema last week.
On Wednesday she welcomed the decision to drop the charges and called for Zambian leaders to show that the country "remains a symbol of peace and a beacon of stability, unity and political tolerance".
A businessman turned politician, Hichilema has claimed he was assaulted by police during his arrest and suffered mistreatment in detention.
Treason is an offence in Zambia that carries a minimum 15-year jail term and, in theory, a maximum sentence of death. A person accused of treason is not allowed to post bail.
Amnesty International said that when he was arrested, Hichilema and the five other accused were "victims of longstanding persecution" by authorities and faced charges designed to "harass and intimidate".
Lungu did not mince words during the election campaign, warning political rivals and activists that "if they push me against the wall, I will sacrifice democracy for peace".