Masterton police Senior Sergeant Warwick Burr said police received a call just after 5pm from a member of the public, who had also contacted KiwiRail.
"They've sort of gone out to the outside of the train and were hanging off the side," Mr Burr said.
Train control contacted the driver who stopped the train, and the youths were pulled into the front carriage by the train manager.
They were held by train staff until they reached Carterton, where they were met by police.
Mr Burr said police had notified the youth's parents of their actions. "[They] weren't particularly happy with their behaviour."
"It's a stupid act where people are putting themselves in danger," he said.
He said the boys had been referred to Youth Aid.
A KiwiRail spokeswoman said under the Railways Act 2005 trespassing can carry a fine of up to $10,000.
"This would be considered trespassing," she said.
She said KiwiRail were thankful they had been contacted as the youths could have been badly injured, or killed.
"It's a highly dangerous thing to do and it's very fortunate that a member of the public alerted us or the result would have probably been horrific. Even if they managed to hold on, the Rimutaka tunnel is very long, it's quite likely that they may not have been able to breathe particularly well in there."
She was unsure whether there had been any incidents of train surfing in New Zealand before.
"I have heard of incidents in Australia which have been horrific.
"It certainly doesn't normally have very good consequences, so we're really, really pleased someone came forward and let us know.
"It is very likely they would have been badly injured if not killed," she said.