24-year-old was looking for shortcut to park when security door of Hilldesheim jail closed behind him

A 24-year-old man who was looking for a shortcut to a nearby park became trapped in a women's prison in central Germany on Monday.

The unnamed man found himself shut inside the walls of Hilldesheim jail near Hannover after entering a metal door on the outside of the prison, only for it to close behind him.

He was eventually rescued after a passing cyclist heard his cries for help. The town's mayor, who also happened to be passing, called the police, but prison officials initially feared the two men were trying to help a prisoner escape – until one of them recognised the mayor and arranged for the "prisoner" to be freed.

Officials at the prison mounted an investigation and discovered that one of three bolts in the door was loose, meaning that it had not closed properly.

"The doors had recently been repaired and one of the bolts was wobbly. These things sometimes happen," said Georg Wessling, spokesman for the ministry of justice in Lower Saxony.

"The man wasn't really locked into the jail," said Wessling. "He just got into an internal courtyard which leads to two further secure prison walls. It's the area visitors pass through, or prison officers when they start their shifts. There was no danger of him encountering any criminals – or of any criminals escaping. He was only in there for a matter of minutes."

The man told prison officials he had made a "blunder" while strolling to a local park, said Wessling. "He said he had been looking for a shortcut."

No crime had been committed, said Wessling. The prison wasn't too embarrassed about the security breach, he insisted. "We see this more as something to laugh about than get angry over."