A hilarious photo captured the moment an alleged burglar got caught hanging on a school fence after his baggy pants got stuck on a spike when he tried to escape.

A locksmith claims he witnessed an unidentified man attempting to force his way inside classrooms at Miles Elementary School in Tucson, Arizona, on Friday, according to reports.

When he realized he had been spotted, the alleged burglar attempted to flee the grounds by jumping over an iron fence surrounding the school.

However, his escape didn't go as planned, as the man's baggy pants got hooked on a spike and exposed his underwear for the world, and police, to see.

An unidentified man was photographed hanging from a fence with his pants down at Miles Elementary School in Tucson, Arizona, on Friday. He was allegedly trying to break into the school when he tried to flee the scene

Passerby Jesse Sensibar saw the whole bizarre and amusing incident unfold and snapped the photo of the man hanging upside on the fence, reported ABC 15.

He posted the photo to Facebook and said: 'One more reason not to jump fences in baggy pants. I saw this homie hanging around at the Miles School this morning.

'I was going to help him off the fence but by the time I got back around the block the cops were rolling up two cars deep.

A passerby snapped the photo of the man hanging upside and wrote on Facebook: 'One more reason not to jump fences in baggy pants' Pictured: Miles Elementary School

'I don't know what his story was but it must not have been good enough, fifteen minutes later when I went back by the other direction going home he was cuffed up in the backseat. He smiled for the camera.'

The school district and the local police department have not commented on this matter yet.

Miles is a small school with grades kindergarten through eighth grade. Average enrollment during the school year is 390 students.