The pilot on the U.S. Airways flight had been allowed to carry the weapon under anti-terrorism precautions.
It went off in the cockpit of the flight from Colorado to North Carolina on Saturday.
Officials said none of the 124 passengers or five crew of the Airbus A319 was injured and the plane landed safely.
> Authorities in the United States have launched an immediate inquiry into the potentially disastrous incident.
Luckily, none of the 124 passengers and five crew members on the flight from Denver to Charlotte, North Carolina, was injured, according to US federal officials and the airline.
The US Airways Airbus A319 plane landed safely in Charlotte, according to a statement issued by the airline.
The incident occurred on Saturday on full flight 1536, which was in the air between 6:45 and 11:51 a.m., according to US Airways officials.
The pilot, who was not identified, was allowed to carry the gun through the U.S. Federal Flight Deck Officer programme run by the Transportation Security Administration, officials said.
The programme trains pilots to carry guns on flights as part of American's anti-terrorist protection policy. It was launched in 2003.
The circumstances of the incident remain unclear. TSA spokeswoman Andrea McCauley said the gun discharged in the cockpit, but she could not release how the gun was being transported at the time.
The TSA issued a statement saying that the plane was never in danger but that the agency and Federal Air Marshals Service take the matter seriously and it is receiving immediate attention.
An airline spokeswoman said the plane has been taken out of service to make sure it is safe to return to flight.
Officials said that the pilot was re-qualified in the programme last November.