|By David Ljunggren

OTTAWA (Reuters) - Kevin Garratt, a Canadian held in China for two years and charged with spying, returned to Canada on Thursday in what was a diplomatic triumph for Prime Minister Justin Trudeau.

The Canadian leader had raised Garratt's case during an official visit to China this month when he both pressed for closer economic ties and openly discussed human rights, which is a sensitive matter in Beijing.

"On Thursday, September 15th, Kevin was deported from China and has returned to Canada to be with his family and friends," the family said in a statement requesting privacy.

A Canadian government official said Garratt had been formally sentenced earlier in the week and then released on bail. He flew into the Pacific city of Vancouver.

In a statement, Trudeau said he was delighted that Garratt had returned safely.

Garratt and his wife Julia were detained in August 2014 near China's border with North Korea. He was charged with spying and stealing state secrets.

Julia, who was not charged, was released on bail and left the country.

A source close to the case, who requested anonymity because of its sensitivity, said Garratt was tried on April 20.

The release came as a surprise, since there were few signs of a breakthrough when Trudeau flew to China. Indeed, while he was in Beijing, the Garratt family expressed their frustration at the lack of progress.

"We raised this case at the highest levels when we were there. It's just indicative of the mature and healthy relationship we have that we can do so," said the government official, who spoke on the condition of anonymity.

The affair had undermined efforts by both countries to boost economic ties. China wants a free trade deal with Canada but opinion polls show most Canadians are cool to the idea, in part because of Beijing's human rights record.

The Canadian official declined to say what the Chinese might have received in return for Garratt's release.

Chinese Premier Li Keqiang is due to visit to Canada from Sept. 21-24.

Brock University professor Charles Burton, a former Canadian diplomat who had served two postings in China, said in an email that Beijing was likely to press for the return of what it has said are corrupt officials who had fled to Canada to avoid arrest.

China does not have extradition treaties with United States, Canada or Australia, which Chinese state media say are the three most popular destinations for suspected economic criminals.

Yang Tianwen, a spokesman at the Chinese embassy in Ottawa, said he was unaware of Garratt's release.

(Additional reporting by Andrea Hopkins; Editing by James Dalgleish and Richard Chang)