A nationwide mail strike threatened for today was averted when the Postal Service and union leaders agreed late yesterday to a compromise procedure for resolving their contract dispute.

With fewer than eight hours to go before a midnight strike deadline for 180,000 letter carriers, chief federal mediator Wayne L. Horvitz announced an agreement that combines the unions' demand for more negotiations and management's call for arbitration.

The agreement, as crafted by Horvitz, calls for a resumption of bargaining followed by binding arbitration if the two sides fail to reach a settlement within 15 days.

Horvitz said he will appoint a special outside mediator who will help with the talks and serve as arbitrator if the talks fail. He said the mediator will be named within 48 hours.

The agreement does not rule out the possibility of wildcat strikes or slowdowns, but both union and postal sources indicated it significantly reduces the chances of a major mail service disruption.

"The nation can now be assured it will continue to enjoy uninterrupted mail service," asserted Postmaster General William F. Bolger, who said in a brief statement that he was "satisfied" with the arrangement.

Only a few hours before, while the plan was under discussion by both sides in Horvitz's office, Bolger told the National League of Postmasters that he would not renegotiate the tentative three-year pact that was rejected by rank-and-file members of the three major postal unions last week.

"I could not in good conscience agree to anything more at the bargaining table now," he told them, reiterating his off-repeated earlier refusals to reopen bargaining.

But, if Bolger had to give ground, so did the union leaders, who had indicated earlier they would abide by official union strike mandates - with deadlines of today for the National Association of Letter Carriers and tomorrow for the 280,000 member American Postal Workers Union - that were aimed at avoiding an arbitrated contract.

Technically, however, the mandates were met. As approved at militant spirited union conventions this summer, they called for a strike if the Postal Service hadn't returned to negotiations with five days of a rank-and-file rejection vote - or if the talks failed produce a new contract within another 15 days.

Some fear was expressed privately yesterday that strike threats might resurface after 15 days if a settlement wasn't reached by negotiation or if an arbitrated settlement was deemed unsatisfactory.

Union leaders have said that any new contract would have to include more money than the 19.5 percent pay increase over three years that was offered in the rejected contract. The rejected money package was considerably lower than other major settlements in recent months and had been considered one of the few achievements of the Carter administration's anti-inflation effort.

Union leaders declined to say what their new demands will be, but presumably they will seek more money. In comments to reporters earlier in the day. Bolger said "the money simply isn't there for higher pay" if postal rates are to remain stable over the next 2 1/2 to 3 years, but declined to say that any wage "sweeting" would cause a rise in postal rates.

Although the dispute had seemed intractable on the surface, with both sides taking hard-line opposing positions, pressures to avoid a mail strike - which would have been the first since an eight-day walkout by nearly 200,000 postal workers in 1970 - were intense.

Postal strikes are illegal, and the APWU and the NALC, with a combined membership of more than 450,000 out of 550,000 nonsupervisory postal workers, were put under a federal court order last Saturday not to strike. Union officers were under a threat of fines and jail sentences if they failed to take positive action to avoid a strike. NALC President J. Joseph Vacca conceded last week that his union's strike fund was virtually empty.

Moreover, both Vacca and APWU President Emmet Andrews are involved in reelection contests, accentuating their need for a compromise that avoided capitulation.

The Postal Service was under unspoken, if not spoken, pressure from the White House and Congress to keep down postal costs and vowed to uphold the no-strike law to the hilt, even though it calls for the firing of all strikers, presumbly including the entire work force if it struck in a body.

Moreover, Bolger told the postmasters yesterday that a shutdown would invite Congress to repeal the Postal Service's monopoly on first-class mail delivery, turning profitable operations over to private carriers and leaving the deficit-plagued postal service with the "dregs - the out-of-the-way and hard-to-process pieces that hold no potential profit for anyone." With the loss of business, he said, would go the loss of jobs.

The government had been prepared to seek a presidential emergency order permitting federal troops to sort and deliver the mail, but snarls were expected that could have caused severe political repercussions, both for the Postal Service and the administration.

Aside from early efforts by White House inflation fighters to push for a low-cost settlement the administration stayed out of the dispute. President Carter, who is vacationing in Wyoming, told reporters he had talked with Labor Secretary Ray Marshall about the dispute by telephone yesterday but declined to comment further.

The procedure invoked by Horvitz, who personally mediated the dispute, has been used before in rail, longshore and other disputes but not often.

"It was important to avoid an unnecessary confrontation," he said at a late afternoon press conference, flanked by the presidents of the three unions and Deputy Postmaster General James Conway, who acknowledged their acceptance of the agreement.

Horvitz carefully characterized the new procedure as a "continuation of the collective bargaining process," a face-saving gesture to both sides.

Vacca appealed to members of his union to stay on the job and said he believed they would do so.

As outlined by Horvitz, a new contract would be submitted for rank-and-file ratification only if it is approved by both sides within the 15-day period. If any issues are unresolved, the arbitrator will make all final determinations, without a ratification vote.

AFL-CIO President George Meany, who had criticized the earlier contract and was cited by some angry postal union leaders as a factor in the contract's rejection, hailed yesterday's agreement as "in the true tradition of collective bargaining and trade unionism."