Five Western nations, including the United States, have reached agreements in principle with South Africa on how the territory of Namibia is to achieve independence by the end of next year.

Diplomats from the five nations met today with U.N. Secretary General Kurt Waldheim to begin the equally difficult task of selling the deal to the United Nations, African nations, and the South-West Africa People's Organization (SWAPO), the liberation movement that has been fighting South Africa for control of the territory.

If the deal proves acceptable to the Africans, it would be a major triumph for the new American policy toward Africa. A peaceful transition in Namibia (Southwest Africa) would demonstrate the credibility of the Carter administration argument that it can offer a nonviolent path to the solution of the racial crises of southern Africa.

South Africa has ruled the territory under a mandate from the League of Nations that was revoked by the United Nations. South Africa has ignored the U.N. action.

The diplomats from the United States, Britain, Canada, France and West Germany, who carried out the latest round of negotiations in Cape Town with the government of Prime Minister John Vorster, were cautiously optimistic, recognizing that it may prove difficult for the Africans to accept the terms South Africa is offering.

These include South Africa's appointment of an "administrator general" to run the territory until independence. He would be a South African judge, empowered to set up the election of an assembly that would draw up a constitution.

The administrator would report to the South African president, who would have full control over the transition to independence, and could deal with SWAPO officials.

One issue that has not been resolved, and may generate opposition to the deal from SWAPO and from Afican nations, is South Africa's insistence on maintaining its troops in Namibia "so long as a threat exists" to the security of the territory.

SWAPO has demanded that the troops pull out, but the Vorster government has refused.

South Africa seemed to have left in limbo plans to establish a provisional government composed of tribal chiefs and white residents loyal to the Vorster government.

Under the agreement, Waldheim will be asked to name a special representative to supervise the election process on the spot, backed by a team of U.N. observers.

The Western diplomats say South Africa pledged to repeal certain legislation that could interfere with the election process, such as racial-discrimination laws.

The U.N. representative and the South African administrator would have to agree on details of the release of political prisoners and the return of exiles.

The Western diplomats said South Africa had agreed in principle to let international jurists decide just who is a political prisoner.

SWAPO had demanded that all prisoners, including those deemed by South African authorities to be criminals, be released.

"We have the principles in place," said one of the Western negotiators. "The next step is for the Africans to agree to them. We can't work out any more ofthe practical details until the U.N. team is appointed."