A pilot who accidentally locked himself in the bathroom of his LaGuardia-bound plane caused a terror scare last night when a helpful passenger with an accent tried to come to his rescue by banging on the cockpit door.

The embarrassing comedy of errors began when the captain of a Chatauqua Airlines flight from Asheville, N.Car., decided to take a bathroom break before landing.

But when he tried to get out of the men’s room, the door jammed, trapping him in the tight quarters.

Desperate to get out and land the plane — which was in a holding pattern above the airport — he pounded his fists on the door to attract attention.

A well-intentioned passenger sitting in the front row heard his thumping and hurried over to help.

Relieved, the pilot told the passenger to go to the cockpit and alert the crew to his plight.

But crew members didn’t react well to the unexpected visit from a stranger trying to breach the highly secure area of the plane.

The jittery co-pilot — at the controls and wondering why his boss’ bathroom break was taking forever — thought the unfamiliar accent was Middle Eastern, a source told The Post.

Practically stammering, he quickly radioed air traffic control.

“We are 180 knots 10,000 [feet] uh, can we leave the frequency for a minute? We are going to try to, uh contact dispatch,” he said.

“The captain disappeared in the back, and, uh, I have someone with a thick foreign accent trying to access the cockpit.”

Even after the passenger explained through the door that the captain was locked in the john, the co-pilot was still suspicious.

“What I’m being told is he’s stuck in the lav [lavatory], and, uh, someone with a thick foreign accent is giving me a password to access the cockpit,” he said.

He clearly did not believe the passenger.

“I’m not about to let him in,” the nervous co-pilot told the LaGuardia tower.

The controller, also spooked, advised the pilot to declare an emergency and “just get on the ground.’’

The captain finally extricated himself and told his colleagues all was well.

Before that happened fighter planes were alerted, although they were never scrambled.

When an air traffic controller called to check if “there any level of disturbance on the airplane,” the pilot got on and responded “negative.”

“The captain, myself, went back to the lavatory and the door latch broke and I had to fight my way out of it with my body to get the door open ,” he said.

“There is no issue, no threat.”

The FBI and Port Authority cops met the plane when it landed around 6:30 pm.

A spokesman for Chatauqua — which runs the regional flight under the Delta name — said cops talked to the passenger and quickly realized it was all a misunderstanding.