UNDER the grim conditions of captivity in Evin prison in Teheran, Iran, a physician found what he believes to be a new and remarkable treatment for the pain of peptic ulcers.

The treatment is simply several glasses of water taken at prescribed regular intervals. Dr. F. Batmanghelidj discovered the treatment largely by accident, but was able to examine about 3,000 patients and follow the medical fate of more than 600, mostly fellow prisoners.

''I was lucky to have been able to make my observations ... when I was waiting clarification of my own situation,'' Dr. Batmanghelidj said in a guest editorial in the Journal of Clinical Gastroenterology. A prisoner at Evin from November 1979 to May 1982, he now lives in the United States.

It started with one patient suffering unbearable ulcer pain late at night. The doctor treated him with 500 cubic centimeters (about a pint) of water, evidently because nothing else was available at that hour.

''His pain became less severe and then disappeared completely after eight minutes,'' said the report.'' The physican was so impressed that he prescribed two glasses of water six times a day and achieved a ''clinical cure'' of the ulcer attack during the patient's stay of a few months in the prison. After that, Dr. Batmanghelidj used the treatment in other prisoners, reducing the amount to one glass half an hour before eating and another glass two and a half hours later.

Gradually, the treatment came into use throughout the prison as its effectiveness became clear in patients whose chronic ulcers were exacerbated by the stress of prison life.

Dr. Howard M. Spiro, editor in chief of the journal, conceded in a note that the treatment might have worked because the patients hoped it would, and that some might not really have had ulcers at all. But he said the observations were ''very important'' and that they ''emphasize what an indomitable clinical observer and investigator can learn even in prison.''

Dr. Batmanghelidj said the almost total lack of demand for antacid from the pharmacy during the last few months of his stay was an added clue to the treatment's effectiveness.