Penang police chief Abdul Rahim Hanafi said the dead men were Mohd Fairuz Adam, who was also known as Joe Mafia, and Mohd Noor Mustafa, a member of the Joe Mafia gang.
"A preliminary investigation revealed that they had attempted to flee the state," he told reporters at Bukit Mertajam.
He said the car they were traveling in, and the pistols they had used had been obtained from members of the Joe Mafia gang still at large.
Abdul Rahim said the police received a tip-off at 12.30pm that the two detainees were in the Kubang Semang area and began tailing them 15 minutes later.
"The police ordered them to stop their car, but they refused and sped off towards Kampung Padang Ibu near Kampung Pelet.
"They drove onto a side lane with the police giving chase, for up to three kilometres. Then the police shot at the car's tyres, forcing the driver to lose control of the vehicle and subsequently crashed into a canal.
"The two men started firing at the police and a shootout occurred," he said.
He also said that the police had received a tip-off that a third detainee was seen buying food at a petrol kiosk along the Kulim-Butterworth highway near Bukit Mertajam at about 2am.
Abdul Rahim said the seven detainees moved about in two groups, one comprising the two Joe Mafia gang members who were shot dead today, and the other comprising the remaining five.
"We advise those still at large to give themselves up," he said.
Abdul Rahim said the police had launched a massive hunt for the five other detainees by deploying 250 personnel from the air unit and the General Operations Force as well.
He advised the people not to shelter the detainees, saying that they could be charged with harbouring criminals.
"Police investigation showed that members of the public had given them food such as bread. We hope the public will cooperate with the police," he said.
At about 9am on Monday, nine detainees being transported in a police van from the Penang prison to the courts complex in Butterworth caused a commotion, blinding the escorting policemen with curry powder when they stopped the vehicle to investigate at the outer ring road, then beating them up before fleeing with the van.
Two of the nine were arrested as they stood by the van which was abandoned in Kampung Padang Chempedak, Tasek Gelugor. The other seven fled towards an oil palm plantation in Ara Kuda.