A consumer research group said yesterday that nearly a quarter of the pacemakers implanted in patients by Maryland hospitals in a recent two-year period were unnecessary, resulting in needless cost and medical risk to the patients.

Public Citizen's Health Research Group, after reviewing the records of 2,222 patients who received pacemakers in Maryland during 1979 and 1980, said the doctors' own diagnoses failed to justify the surgery in 20 to 25 percent of the cases.

The group, in a joint study with Philadelphia cardiologist Peter R. Kowey, said the unnecessary operations in Maryland cost $2.8 million a year. If the Maryland figures indicate a national trend, the group said, nearly 25,000 of the 100,000 pacemakers implanted every year in this country are being done needlessly, at a cost of more than $280 million.

A pacemaker is a mechanical device, implanted in the chest, that transmits electric impulses to help an ailing heart beat normally. The average cost for the procedure, including surgeons' fees, was about $8,600 in Maryland in 1980.

The group reviewed these operations by checking the records of Maryland's Health Services Cost Review Commission, which regulates hospital costs in the state. Dr. Sidney M. Wolfe, the group's director, said he used broad categories in judging whether the pacemakers were needed and gave the physician the benefit of the doubt if the diagnosis appeared incomplete, as in one case where the patient was listed as fainting.

Nevertheless, Health Research Group said up to a quarter of the operations were clearly inappropriate and another 12 to 14 percent should be considered questionable. For example, the group said patients who had suffered certain kinds of partial heart blockage should not have been given pacemakers unless some other ailment was present.

These patients were needlessly exposed to the risks involved in pacemaker surgery, such as infections or clotting in major blood vessels, the group said.

In a letter to Health and Human Services Secretary Richard S. Schweiker, Wolfe suggested that Medicare require a second opinion before paying most of the cost of pacemaker surgery. Wolfe also said federally funded peer review groups should examine these procedures more closely.

In an earlier study at Brooklyn Hospital, the group said, the number of pacemakers implanted was found to have dropped sharply after 1977, when the hospital asked other physicians to review each procedure.