Over the objections of US prosecutors, a judge agreed to free former IMF chief Dominique Strauss-Kahn from jail on $1m (€698,209) bail on the condition he be confined to a New York apartment under armed guard while he awaits trial on attempted-rape charges.
The 62-year-old French banker and diplomat briefly wore an expression of relief after Supreme Court Justice Michael J. Obus announced his decision in a packed courtroom.
Later, Strauss-Kahn blew a kiss toward his wife.
The ruling did not immediately free Strauss-Kahn from the city's bleak Rikers Island jail.
Authorities need time to review the security arrangements involved in his house arrest, which lawyers said would be at an apartment rented by his wife.
The lawyer who represented Strauss-Kahn at the hearing, William Taylor, called the ruling "a great relief for the family".
"He's going back to Rikers tonight and we expect him to be released tomorrow," he said.
Strauss-Kahn will not only have to post the full $1m but will also have to take out a $5m (€3.5m) insurance bond. A trial date was not immediately set.
The banker is accused of attacking a 32-year-old housekeeper on Saturday at his $3,000 (€2,094)-a-night hotel suite. The West African immigrant told police that he chased her down a hallway, forced her to perform oral sex and tried to remove her stockings.
He spent nearly a week behind bars - most of that at Rikers, after a judge denied him bail on Monday. At that hearing, prosecutors warned that Strauss-Kahn might flee to France and escape justice in the US like film director Roman Polanski.
This time, Strauss-Kahn went before a different judge, and also offered to place himself under house arrest. Mr Obus added the requirement that he post the $5m (€3.5m) insurance.
The bail decision came less than a day after Strauss-Kahn resigned as managing director of the International Monetary Fund, the powerful organisation that makes emergency loans to countries in financial crisis.
In his resignation letter, he denied the allegations against him but said he would quit in order to "protect this institution which I have served with honour and devotion" and to "devote all my strength, all my time and all my energy to proving my innocence".
Also, prosecutors announced that Strauss-Kahn had been formally indicted on the sex charges. Without the indictment, authorities would have been unable to detain him for longer than a week.
Strauss-Kahn did not speak during the court proceedings, but Mr Taylor said his state of mind was "much better now than before we started".
Prosecutors with the Manhattan district attorney's office had argued against his release, citing the violent nature of the alleged offences and saying his wealth and international connections would make it easy for him to flee.
"The proof against him is substantial. It is continuing to grow every day as the investigation continues," assistant district attorney John "Artie" McConnell told the judge. "We have a man who, by his own conduct in this case, has shown a propensity for impulsive criminal conduct."