For about 10 minutes this afternoon on the muddy banks of the overflowing St. Mary's River here, there was the following spectacle:

The president of the United States, clad in white shirt, crisp black suit and low-cut, rubber boots borrowed from a farmer, standing in a line of people to help pass sandbags up to the river's edge as throngs of onlookers, reporters and cameras watched.

President Reagan had decided on the spur of the moment to stop briefly in this flooded industrial city on his return to Washington from a two-day southern visit. Reagan acted after seeing morning television reports that northern Indiana's worst flooding in nearly 70 years had washed away crops, inundated houses and businesses and left more than 7,000 people homeless, including 4,000 evacuated late tonight.

Reagan's stop generated far more excitement than any of those in his two-day swing through the South where he had promoted his economic program and "New Federalism" proposal in speeches to the legislatures of Alabama, Tennessee and Oklahoma.

For the tired residents of this economically depressed city, who have been laboring since Saturday against flood waters, Reagan's unexpected visit seemed to provide a brief moment of psychic relief.

Drawn by radio reports and the flashing lights of the presidential motorcade, they tumbled out of houses, taverns and schools. Small boys climbed trees, and adults sat on rooftops to catch a glimpse of Reagan as he stopped first at the river bank and then at a Red Cross station where homeless families crowded around him to get autographs.

The president said that the silver lining of the disaster lay in the spirit of neighborliness it generated as hundreds of volunteers gathered to help fight the flood and help its victims.

Standing on a table inside a temporary Red Cross center at a church, Reagan told evacuees: "I think you'd all be very proud to see those youngsters up there pitching those sandbags. I did it for a couple of minutes, then stepped in some sticky mud and lost a boot."

He told them that the volunteerism exhibited here "makes me realize we haven't got anything to be afraid of in this country."

Although President Carter frequently traveled to disaster sites, today's unscheduled stop was the first such visit for Reagan. It came at a time when critics are attacking him for appearing uncaring and for seeming to show a lack of compassion for the poor and unemployed.

Such criticism clearly concerns Reagan and his aides.

In his southern trip, Reagan was unbending toward critics urging him to revise his economic recovery program, but he made a point of saying that he understands the problems of the jobless laborer searching for work in a tight market, the young couple unable to buy a home because of high interest rates, the elderly pinched by inflation and disadvantaged youths who may feel that government has turned its back on them.

"We are determined to change things for the better, to make America work again," Reagan had told the Tennessee legislature. "But we can't do that through more spending and taxing. That's how we got into this mess to begin with."

While he was received with warmth and politeness by members of the three legislatures, his visits drew only sparse crowds of friendly supporters and some demonstrators protesting budget cuts, Central American policy, government treatment of Indians and assorted other concerns.

The Reagan entourage moved into high gear this morning for the Fort Wayne visit. En route here after addressing the Oklahoma legislature, Reagan had Air Force One detour over Bartlesville, Okla., where a tornado Monday night left one dead, more than 35 injured and caused widespread damage.

Photographers on the plane were permitted to enter the cockpit to photograph Reagan as he inspected the damage.

Both Air Force One and the charter flight carrying the White House press corps were instructed to fly low on the approach to Fort Wayne so the president and reporters could see the flooding that had caused an estimated $16.3 million in damage after beginning Saturday when heavy snows melted in a sudden thaw.

Reagan's visit was so spontaneous that Bruce Hetrick, an aide to Fort Wayne Mayor Winfield Moses (D), said Moses' office had not been aware of it until calls came from news organizations.

The city had been trying since the weekend to get Indiana Gov. Robert D. Orr (R) to ask the administration to declare Fort Wayne a federal disaster area so farmers and businessmen would be eligible for low-interest government loans. Orr submitted the request Monday, according to a White House aide, and Reagan today promised speedy action on it.