The man, with dual Romanian-Hungarian nationality, was asked by security officers at the gate into the Mountjoy Jail complex whether he was carrying anything prohibited.
“The man said ‘I have this’ and took out a gun,” said a prison source.
The weapon turned out to be a loaded Walther semi-automatic pistol with 14 rounds of ammunition.
The security officers immediately alerted gardaí, based in Mountjoy Garda Station, 100 yards away.
The 37-year-old man was driving a waste disposal truck and was going in to collect a skip from St Patrick’s Institution for Young Offenders, which is located in the Mountjoy complex.
Gardaí rushed to the scene and arrested the man and took custody of his weapon.
A spokesman for the Prison Service said: “At approximately 10.25am, as part of routine security measures, a targeted vehicle search was carried out by the Operational Support Unit of St Patrick’s on a waste disposal truck.
“During the search, a handgun, two magazines and 14 rounds of ammunition was found in the possession of the driver.”
He said the truck was impounded by the gardaí.
He said at no point was the vehicle on prison grounds and remained outside the main entrance.
The man was arrested under section 30 of the Offences Against the State Act and questioned at Mountjoy Garda Station.
A Garda source said: “He did not thinking carrying a semi-automatic pistol was a big problem.”
He pointed out that it was illegal to carry around a weapon back in Romania or Hungary.
It is not clear yet if there was any particular reason why the man was carrying around a loaded weapon.
Prison and Garda sources are confident the man was not attempting to smuggle the handgun in.
Fine Gael justice spokesman Charlie Flanagan said the incident was “extremely worrying”.
“It is a concern that a person feels the need to carry a pistol around for his own protection. Prison authorities must provide assurances that anybody who is granted access to prison grounds has full security clearance and should clarify whether this was so in this case.”
The Prison Service said anyone entering the prison – including refuse collectors – were subject to security clearance.
The handgun is being examined by the ballistics experts at the Garda Technical Bureau.