As if by magic, the Hogwarts Express has come to the rescue of a stranded family in Scotland.

The train that took Harry Potter to school was played onscreen by the Jacobite steam train, which runs on a remote and scenic route through the Scottish Highlands.

On Friday, it made an unscheduled stop to pick up a family of six that was stranded when a storm washed away their canoe.

Jon Cluett, his wife and four children between the ages of 6 and 12 were staying in a lakeside hut on Loch Eilt when they awoke to find their canoe was gone.

Faced with walking several miles over boggy ground to get back to the family car, Cluett called police to see if any form of rescue was available.

“The policeman said, ‘We’ve arranged for the next train passing to stop for you, and you’re not going to believe this but it’s the Hogwarts Express steam train. Your kids are going to love it,’’” Cluett said Sunday.

Cluett said his children, all Harry Potter fans, were “really excited” by the adventure.

“They know the Harry Potter films and they know that are filmed in the Highlands,” he said. “But they hadn’t put all of that together in their heads until they saw the train.”

Cluett, the pastor of a church in Stirling, central Scotland, is hopeful someone will find his canoe and give the story a perfect happy ending.

“It’s got to turn up at some point. The thing is 16-foot-long, red and floats,” he said.