A judge in Dakar ordered his "provisional release on bail, his passport withdrawn, a travel ban and a ban on commenting on the case," lawyer Cire Cledor Ly said in a message to AFP.
Niang went on a hunger strike on December 2 to protest his imprisonment, and was taken to a clinic earlier this week after his health deteriorated, according to his defense team.
He was discharged on Tuesday and taken to prison again, local media reported.
Niang, the head of the Dakar Matin online news site, was arrested on November 6 and charged on November 9 with "divulging information likely to harm national defense".
He was also accused of "receiving administrative and military documents" and "spreading false news likely to bring public institutions into disrepute".
He is accused of having broadcast confidential messages about security arrangements surrounding the November 3 interrogation of opposition politician Ousmane Sonko over allegations of rape.
Such messages were widely shared by others on social media.
Investigators and Niang's lawyers did not say what documents were being used against him.
The journalist's detention sparked a wave of criticism from the press and civil society groups against the authorities.
Many human rights organizations and the opposition had called for his release.
Senegal ranks 73rd out of 180 countries in RSF's latest press freedom index.
The country lost 24 places compared to 2021.