HONG KONG — The Hong Kong democracy activists Joshua Wong and Nathan Law were released from prison Tuesday after the city’s highest court granted them bail to appeal sentences for what their supporters called political prosecution by the government.

“The world is watching the result of the case, and I hope that the result of the case showcases the protection of human rights in our law system,” Mr. Law told reporters on Tuesday afternoon as he walked free for the first time in two months.

Mr. Wong and Mr. Law were sentenced in August to six and eight months for their roles in a demonstration three years ago calling for freer elections in Hong Kong, a semiautonomous region of China. The Beijing-backed local government had pushed for the prison sentences as a deterrent.

The length of the sentences left the young protest leaders ineligible to seek public office for five years, prompting a huge protest in which supporters called them political prisoners.