DUBLIN, Oct. 20— Irish fishery patrol boats opened fire on a Spanish trawler in a confrontation that developed after the vessel was found fishing illegally in Irish waters, Irish officials said here today.

According to the Spanish Ambassador, the fishing boat, the Sonia, sank after being hit by the gunfire, the Spanish news agency E.F.E. reported. But this could not be independently confirmed.

The vessel had radioed that it was taking on water, and British Coast Guard officials said all 16 Spanish crewmen were rescued before dawn off Land's End in southwest England. No casualties were reported.

Irish Defense Ministry officials said the patrol boats opened fire on the Spanish ship after it tried to ram one of the Irish boats during a five-hour chase that began Friday night in a gale. Cannon Shells Are Fired

The Defense Ministry said in a statement that the Irish vessels had fired a total of 596 rounds, ranging from small arms ammunition to 20-millimeter cannon shells.

An Irish Defense Forces spokesman said the incident began when the fishery-protection vessel Aisling discovered the 330-ton Sonia off Wexford in southeast Ireland.

The Irish spokesman said the Aisling and another fishery-protection vessel tried to put a boarding party on the Sonia, but were unable to board it because of rough seas and the unpredictable course of the Spanish vessel.

The spokesman maintained that the Spanish ship had ignored all internationally recognized instructions to halt. Ambassador Notified Quickly

Luis Jordana de Pozas Fuentas, the Spanish Ambassador, told the Spanish news agency that he had been notified by the Irish Navy soon after the incident started. He relayed a message to the trawler ordering the captain to surrender to the Irish authorities, the news agency quoted him as having said.

The Irish spokesman said the fishery-protection vessels began firing ''warning'' rounds of live ammunition in the chase across the Irish Sea. It said that although the first shots were fired over the trawler, shots were later aimed directly at it as it made repeated attempts to ram the Aisling.

''During the ramming attempts, the Aisling fired shots into the vessel's bows and at the structural part of the vessel's bridge,'' the statement said. It said the shots were aimed to hit the trawler but miss the crew.

The Irish vessels gave up the chase for fear of causing casualties when the trawler reached British waters, the Irish spokesman said. West Germans Save 3

British Coast Guard said the trawler later radioed a distress call, saying it was taking on water in heavy seas and gale-force winds and was in imminent danger of sinking about 45 miles north of Land's End.

A West German cargo ship, the 995- ton Achat, rescued three members of the trawler's crew. The other 13 were rescued by a Royal Air Force Sea King helicopter sent from its base at Brawdy in South Wales. Three other British vessels and another helicopter were also sent to the scene.

The Press Association, the British domestic news agency, said the three trawler crewmen taken aboard the Hamburg-registered Achat were being taken to the Irish city of Waterford. The agency said the other 13, lifted to safety by the British helicopter before dawn, were taken to the Royal Air Force base at St. Mawgan in southwest England.

One of the 13 Spaniards taken to St. Mawgan said one of the Irish boats had opened fire without warning. 'No Warning Shots' ''They fired directly on our boat and there were no warning shots,'' said Sabino Zubichray, 40 years old.

Mr. Zubichray acknowledged that the Sonia was fishing inside Irish waters when the incident began Friday. But he said: ''Reports that we tried to ram the Irish ship are rubbish. It would be impossible. We had only a small fishing boat and wouldn't try anything like that.'' He said he knew of no guns aboard the Sonia.

Speaking through an interpreter at the St. Mawgan base, Mr. Zubichray said: ''Our ship was hit everywhere. All the glass was broken. The bridge was shot to pieces. The ship was covered in holes, some small, some big.''

He said: ''We couldn't communicate with the Irish because all our radio equipment was destroyed when they opened fire. They fired immediately, and no warning was given.'' Official Inquiry Planned The spokesman for the Irish Defense Forces said Irish Government officials would get in touch with the Spanish Consul here about the affair. He said they also planned an official inquiry.

Because Spain is not a member of the European Common Market, it is banned from fishing in the Community's waters.

On March 7 two Spanish trawlers, the Burgoamendi and Valle de Atxondo, were fired on by a French Navy gunboat while illegally fishing in French waters. Nine fishermen were wounded, two seriously.