For the first time in two years, the number of Americans facing loss of their homes through foreclosure has declined, according to data collected by the Mortgage Bankers Association of America.

In addition, the number who are behind on their payments has also dropped, the trade group found.

The brighter picture is due primarily to improved economic conditions generally, "to the same things that have caused employment to improve" during the spring and summer, Mark Riedy, the association's executive vice president, said yesterday.

Riedy noted also that the "very strong real estate market this spring probably allowed some people to sell their homes in a favorable climate to pull themselves out of the throes of foreclosure."

Riedy added that he foresees a slight decline in interest rates by the end of the year that, coupled with continued economic recovery, should mean a steady reduction in foreclosures and delinquencies at least through the early part of next year.

And in the report accompanying its figures, MBA said, "The outlook for the mortgage delinquencies and foreclosures is bright for the remainder of 1983. The economic recovery appears to be on a solid footing. . . . The recent uptick in interest rates is a cause for concern, but it does not appear to be large enough to seriously threaten the economic recovery."

Riedy cautioned, however, that "we mustn't lose sight of the fact that we are coming off an all-time high for the MBA survey , so there isn't too much to crow about."

He noted that "the problem will linger on in some states" where unemployment has been particularly high, and added that if a federal aid program is passed it should be targeted to these needy areas.

Foreclosures, which rose last winter to the highest level in more than a decade, have been a major issue on Capitol Hill, with Democrats pressing for a federal aid program and Republicans--and the Reagan administration--arguing that it was not needed.

The House in May passed a $760 million program to provide government loans to unemployed homeowners facing foreclosure. The Senate included a similar provision in its housing authorization bill, which is currently facing an uncertain fate on the Senate floor.

The MBA figures, which are for the second quarter of the year and cover 8 million loans totaling an estimated $265.7 billion, showed that 5.63 percent of all mortgages were delinquent, down from 5.84 percent in the previous quarter, the highest rate ever recorded by MBA.

Loans actually in the process of foreclosure totaled 0.66 percent, down from 0.71 percent in the first quarter, also a survey record.

The numbers mean that about 10,000 fewer homeowners faced loss of their homes than in the first quarter of the year, the MBA said.

Only the troubled industrial north central states failed to record any improvement, with the delinquency rate in that region climbing from 5.64 to 6.10 percent. Illinois had the highest rate at 7.24 percent, while Alaska was the lowest at 2.73.

Among local jurisdictions, the District had the highest delinquency rate, 7.22 percent, while Maryland's rate stood at 3.73 and Virginia's at 3.32.

The MBA figures painted a somewhat brighter picture than data released Monday by the Federal Home Loan Bank Board, which indicated that after a decline in June, delinquencies turned up again in July to a rate about the same as that in April and May.