A disaster was averted Friday by a quick-thinking air traffic controller. Two American Airlines jets were flying with the same flight number and the controller made some quick decisions to avoid confusion and keep them apart.

Audio of the incident, which happened shortly after midnight on Friday, shows how the controller found a solution to make sure that the planes — which were both called American 2482 — maintained proper separation. One plane was arriving from Chicago shortly after the other had taken off from Phoenix, Arizona.

“I’ve been working air traffic for 25 years. I’ve never seen two aircraft pretty much merge with the same call sign. That’s pretty incredible,” the controller told the Associated Press after the departing plane safely cleared the area.

• Chilling audio captures moment passengers threaten to take over Alaska Airlines flight

• Passenger on LA-bound flight dies in the air after suffering medical episode

American Airlines flight prepares to take off.

The controller ensured that both pilots were flying at different altitudes. If they thought that the controller was telling them to fly at the same altitude, they might have collided, so it was critical that they knew which plane he was directing.

The audio, posted by ATC.com, shows exactly how the controller quickly shifted to identifying one plane as “American 2482 on the arrival” and the other as “American 2482 on the departure,” ensuring that the two planes maintained a safe course.

The controller kept the arriving airplane safely above the departing one, even as their flight paths converged. He also made sure the pilots of both airlines could see each other even when they were still miles apart. He directed the departing plane not to climb too high, so the planes would still have a large gap between them.

American Airlines flight prepares to take off.

The arriving pilot complimented the controller for his “nice work” once the planes had passed each other safely.

“It could have been disastrous, but I’m glad it worked out,” the controller replied.

A Boeing 737 American Airlines passenger aircraft (foreground) and a Airbus A320 American Airlines passenger aircraft (center) are seen at a terminal at LaGuardia airport in New York City.

Aviation safety expert Steve Arroyo noted that it’s likely someone inside the airline’s dispatch center missed the fact that both planes would be in the air at the same time with the same flight numbers. Generally, both the dispatcher and the pilots sign off on a flight plan.

“You can drop the ball and that shouldn’t happen, but it’s nice to see that the system is resilient and we have all the safeguards,” Arroyo said. The AP adds that he's only seen this type of issue a few times in his four decades with United Airlines. Arroyo started his career as a dispatcher before becoming a pilot.

A traveler looks at the arrivals and departures board at Detroit Metro Airport.

The Federal Aviation Administration and American Airlines will investigate how this happened, and they both emphasized how glad they were that everyone remained safe.

“The diligence and professionalism of air traffic controllers and our pilots ensured required separation was always maintained and the issue was identified and addressed. Both flights continued safely and without incident,” American Airlines said in a statement.