The nation's largest railroad labor union reached a tentative agreement on a new contract with the railroads yesterday morning as months of bargaining ended after a 26-hour session with the National Mediation Board.

If ratified, the pact is expected to set a pattern for the 12 other national rail labor unions still in negotiations and to reduce but not eliminate the possibility of a rail strike.

The railroads, like airlines and trucking companies, are seeking major productivity gains to trim their costs in the more competitive environment fostered by transportation deregulation legislation.

Details of the agreement between the United Transportation Union and the railroads were withheld pending the ratification vote expected by the end of July. The UTU represents about 90,000 railroad employes, more than one-fourth of a total national rail work force of about 350,000. UTU's members are conductors, trainmen and firemen.

The National Mediation Board, the federal agency that handles collective bargaining involving railroads and airlines, would say only that "the new agreement covers wages, benefits and work rules."

Sources said, however, that the agreement provides UTU's members with an immediate bonus for ratification, a guaranteed wage increase of about 10.5 percent over the 40-plus months of the agreement, and cost-of-living protection beyond the 10.5 percent. In 1983, the last year for which figures are available, the average railroad employe earned $32,125, or $13.29 an hour. The UTU had originally sought a 30 percent pay raise over three years.

The UTU tentatively agreed to significant cost-saving changes in their contracts, the sources said. The changes include a two-tier pay scale, on which new employes will be paid less than current employes; a major increase in the number of freight trains the railroads can operate without cabooses, which translates into fewer employes on board each train; and an increase in the number of miles considered a day's work for a crew.

The two-tier pay scale -- the type of proposal that precipitated the recent United Airlines strike -- has become a favorite of transportation companies. They can preserve existing salaries while cutting long-term costs by paying newcomers at a lower scale. The tentative agreement reportedly has a second tier paying about 75 percent of existing wages, with the two tiers merging after five years.

Under the current contract, railroads can eliminate cabooses from as many as one-fourth of their through freights; under the tentative agreement cabooses could be eliminated from unit trains and piggyback trains, the two fastest-growing segments of the industry.

A unit train carries one commodity -- usually coal -- between two points, without making intermediate stops. A piggyback train carries truck trailers on flatcars, thus cutting out the truck driver for the long haul.

Under the current contract, a crew is paid for a day's work after a freight travels 100 miles, one of the most arcane rules in railroad labor and one the companies have wanted to change for years; they proposed 160 miles. Sources said the settlement was 108 miles, amounting to an 8 percent cost saving.

Charles I. Hopkins Jr., chairman of the National Railway Labor Conference and collective bargaining spokesman for the railroads, said, "The settlement is responsive to competitive realities and also to employe interests." Fred A. Hardin, UTU president and chief negotiator, could not be reached for comment.

The contract tentatively settled yesterday has been open since last July 1 and negotiations have been under federal auspices since December. Although the UTU is the biggest railroad labor union and often the pattern-setter, that is not always the case. In 1982, after the UTU settled, the Brotherhood of Locomotive Engineers struck. Congress imposed a settlement almost immediately.