The four were escorted separately out of Roumieh prison on the outskirts of Beirut in a convoy of vehicles after The Hague-based STL said there was insufficient evidence to charge them.
Speaking from their respective homes after their release, the generals said they had been vindicated by the court and insisted they had nothing to do with Hariri's 2005 murder in a massive car-bombing that also killed 22 others.
"Four generals were freed today but the only remaining inmate is Rafik Hariri and his jailer is Lebanon's justice system," security services director Jamil Sayyed told a crowd of cheering supporters outside his home that included several MPs from Hizbullah.
Saad Hariri agreed to "exchange his father for the sake of politics. He embraced people who do not want the truth to be uncovered," he said, referring to al-Mustaqbal Movement leader.
"I thank everyone who stood by us, and above all Hassan Nasrallah," he said, referring to the head of Hizbullah. "Never in the history of Lebanon have people been jailed for four years with no charge or evidence."
In the ruling, the tribunal's first since opening its doors on March 1, pre-trial judge Daniel Fransen granted a request by prosecutor Daniel Bellemare who said the available evidence was "not sufficiently credible" to hold the generals.
The men "cannot at this stage of the investigation be considered as either suspects or accused persons," said Fransen in a decision broadcast live on Lebanese television and via an Internet webcast.
"As a result ... they do not meet the conditions to be placed in provisional detention."
The four have been detained in Lebanon since 2005, but are legally in the custody of the tribunal since Beirut relinquished its jurisdiction in the Hariri case this month following the official opening of the STL in March.
They are the former head of the presidential guard, Mustafa Hamdan, 53, security services director Jamil Sayyed, 58, domestic security chief Ali Hajj, 52, and military intelligence chief Raymond Azar, 56.
Fransen said a key witness had retracted a statement that initially incriminated the generals, undermining the case against them. Bellemare said in court he would not appeal.
So far, Bellemare has not indicted anyone and has not identified any other suspects in the 2005 suicide bombing. But he vowed to continue his investigation.
"Not only should people understand that the investigation is bigger than the case of the four officers, they should also understand that should any of the investigative leads direct us back to them with sufficient credible evidence I will seek their detention and indictment," he said in a statement.
Fransen instructed the Lebanese authorities "immediately to take the measures necessary to ensure the safety" of the four generals, and to free them without delay.
"We worked to establish this tribunal, we have complete confidence in this tribunal and we must therefore abide by all its decisions," Justice Minister Ibrahim Najjar told AFP.
Interior Minister Ziad Baroud said before their release: "We have ordered immediate measures be taken to free the four generals and see them home safely."
Lawyers for the generals said the decision was four years too late.
"After 44 months, justice has been done and it should have been 43 months ago," Naji Bustany, counsel for Hamdan and Azar, told AFP.
Bustany welcomed Wednesday's ruling saying Fransen and Bellemare had shown objectivity and fairness in their decision.
"This is the result I was waiting for a long time ago," Hajj's lawyer Issam Karam told AFP. "I followed the case from the onset and there is nothing in the file that implicates the generals."
After the decision, celebratory gunfire broke out in the southern suburbs of Beirut while relatives of the four generals wept with joy.(AFP-AP-Naharnet)