Ronald Burton was convicted of lewd and libidinous behaviour towards a boy from 1994 to 1996, and of sexually assaulting a 13-year-old boy in 2014.
The 61-year-old had his appeal upheld by Edinburgh's High Court of Justiciary after serving eight months in prison.
The appeal centred of the use of the "Moorov doctrine" for conviction.
The principle means that two or more separate offences, each witnessed by only one person, can be grouped together to show a pattern of behaviour and then used in a court case.
The Lord Justice Clerk, Lady Dorrian said for the rule to apply, the similarity in offences needed to show a "course of conduct systematically pursued by the accused".
She also highlighted that the temporal gap between the offences, at its shortest, was just under 17 years.
Lady Dorrian said: "It was argued that the circumstances were such that the trial judge was correct to leave the decision to the jury.
"However, it has been repeatedly stated that the rule of mutual corroboration must be approached with caution, and this especially when there are only two complainers.
"In the present case there is the added factor of a 17-year interval between the respective accounts.
"We are satisfied that the issue should have been withdrawn from the jury. In the circumstances the appeal must succeed."
Mr Burton, an ex-assistant principal teacher, had maintained his innocence throughout his trial.
A former teacher who was jailed for sexually abusing two pupils at South Lanarkshire secondary schools has had his six-year jail sentence quashed.