Mr Hichilema was due to stand trial, but instead walked free from Lusaka's high court after 100 days in custody.
Sources say the charges against Mr Hichilema and five aides were dropped after a deal was negotiated by the Commonwealth.
He narrowly lost the election to President Edgar Lungu last year.
Mr Hichilema, the leader of the United Party for National Development (UPND), alleges the vote was fixed, and does not recognise Mr Lungu as president.
Mr Lungu, meanwhile, faces accusations of growing authoritarianism.
Mr Hichilema was arrested in April, accused of endangering the president's life after his motorcade allegedly refused to give way to the one transporting Mr Lungu.
He and his aides "strongly" denied the charge, which carries a sentence of at least 15 years. Those found guilty can also be sentenced to death.
But all charges have now been dropped, apparently after a visit by Commonwealth Secretary General Patricia Scotland, who was in Zambia last week and met Mr Lungu and Mr Hichilema.
She later hinted Mr Hichilema could be released in the public interest.
A source had earlier told the BBC he was "definitely being released today... unless there's a last minute development".
Zambia's opposition leader Hakainde Hichilema, who has been held in prison since April on treason charges, has been released.