A sheriff had granted Karl Henderson, 17, an absolute discharge after he pled guilty to the indecent attack on a 15-year-old girl at a secondary school.
However, judges at the Court of Criminal Appeal in Edinburgh have quashed the disposal and ordered that he carry out a one- year community payback order with a supervision requirement.
They also confirmed he would be subject to the notification requirements of the sex offenders' register.
The Lord Justice Clerk, Lord Carloway, who heard the appeal with Lord Menzies and Lord Bracadale, said the case was "sufficiently serious to require some punitive element in the form of a conviction".
The judge said they had been driven to the conclusion that Sheriff John Mundy had placed "undue weight" on the personal circumstances of Henderson when balanced against the offence.
Henderson, of Fankerton, by Denny, in Stirlingshire, followed his victim into a cupboard, exposed himself and carried out a sex act during the assault.