The Student Loan Marketing Association has agreed to manage temporarily the $9 billion in student loans guaranteed by a Kansas agency that was one of the largest guarantors of student loans and the primary one for borrowers in the District of Columbia, the Education Department announced yesterday.

Department officials described the agreement in principle as a solution to the financial troubles of the Higher Education Assistance Foundation (HEAF).

Over the next three years, the $9 billion in existing guaranteed loans will be divided among as many as 20 other agencies around the country that guarantee student loans and guarantors will be found shortly to replace HEAF for new loans.

Since HEAF's financial problems, caused because so many of the loans it guaranteed went into default, were disclosed in July, banks that make student loans have been nervous and concern was raised in Congress that an expensive federal bailout might be required.

Investors also shied away from the Student Loan Marketing Association, known as Sallie Mae, because it owns about $2.5 billion in loans that have been guaranteed by HEAF and has extended credit to the nonprofit agency, based in Overland Park, Kan. Sallie Mae's stock, which sold for $54 a share on July 23, closed yesterday at $42.125, up $2.50 from Thursday. The stock reached a low of about $34 a share after the disclosures.

Education Secretary Lauro F. Cavazos said the three-year agreement would assure students access to loans, prevent any possible spread of the problem and and minimize the cost to the federal government. Undersecretary Ted Sanders said the agreement would cost about $26 million.

Several details of the plan remain to be decided, including which agency will insure new student loans in the District, where HEAF each year has guaranteed about $45 million borrowed by 19,000 students.

Sanders said the department would name an interim guarantor "within a matter of days" after consulting Mayor Marion Barry's office. Sanders identified the Virginia Education Assistance Agency as one candidate, while the Massachusetts Higher Education Assistance Corp. said it has offered its services to the District's major universities.

Department officials said HEAF would continue to guarantee new loans until interim successors are named in the District and five states where the Kansas agency has been the main guarantor.

Last month, Sallie Mae agreed to lend HEAF up to $200 million to meet the agency's cash needs through Fiscal 1990, which ends Sunday. HEAF has repaid that loan, Sanders said.

Under the new agreement, Sallie Mae, which buys guaranteed student loans from the banks that make them, will not guarantee loans. Other agencies complained that if Sallie Mae expanded its operations to include guaranteeing loans, which Sallie Mae proposed last month, competitive pressures and conflicts of interest would be created.

Sanders said agencies from Pennsylvania, Illinois, Massachusetts and the Great Lakes area have offered to take over the biggest share of guarantees issued by HEAF.

Edward Stringer, the department's general counsel, said the agreement erects "a Chinese wall" between Sallie Mae and the $2.5 billion of HEAF-guaranteed loans it owns.

Sallie Mae will also be responsible for trying to collect another $3 billion in loans that have gone into default, financing and managing HEAF's operations, and naming its new board of directors jointly with the Education Department. For its services, Sallie Mae can take up to $20 million from HEAF receipts, department officials said.

"As the largest holder of guaranteed student loans and the largest servicer of such paper, we have developed considerable expertise in managing what are highly specialized processes specific to this program," said Lawrence A. Hough, president and chief executive officer of Sallie Mae.