Another sailor who had been imprisoned along with Captain James, Vijayan, has been released too.
"Sailors from #India Capt Sunil James & Vijayan released from detention in #Togo. Will leave for home later today," Syed Akbaruddin, spokesperson, ministry of external affairs, tweeted this morning.
Mr Akbaruddin said the two men were released after India's High Commissioner in Ghana K Jeeva Sagar met Togo's President Faure Gnassingbe.
The two sailors are with the Ghana High Commissioner right now, he said.
"It came as a surprise. We didn't have hope until yesterday. We are relieved," said Captain James' wife Aditi.
Captain James was arrested by Togo in July on charges of helping pirates who had attacked and looted his ship, MT Ocean Centurion, near the tiny African country that borders Ghana. His family had been making desperate attempts to secure his release since then.
Earlier this month, his 11-month-old son, Vivaan, died of septicemia. The family has refused to do the last rites of the infant till his father has seen him one last time. The baby's body has been kept in a morgue in Mumbai, and the last rites are expected to be conducted after Captain James' arrival.
Last week, Captain James' family met Prime Minister Manmohan Singh to seek his help in getting the Mumbai sailor freed.
"It was a very sad incident. I am grateful to the officials who worked hard for the release," Minister for External Affairs Salman Khurshid said today.
The family has accused the Indian government of not stepping in soon enough to secure his release. It has alleged that Mr James was tortured by the pirates and has also lost 15 kg in the custody of the Togo police.
"Does it take the death of a baby for the government to act?" his brother-in-law Rakesh Madappa had asked bitterly last month.
Captain James' wife Aditi had said about him last week, "He is not doing good there with the shock of losing a child. He is devastated."
The Indian government has said it has followed Captain James' case since his arrest.