The nation's largest railroad labor union, whose first tentative contract agreement with the railroads failed to win members' ratification, reached a second pact yesterday, perhaps reducing the possibility of a national rail strike at midnight Oct. 25.

Details of the United Transportation Union (UTU) agreement were withheld pending completion of the union's ratification process.

That is expected to take "three to four weeks," according to a joint news release by the railroads, the union and the National Mediation Board, the federal agency that assists collective bargaining for railroads and airlines. None of the principals would comment.

The UTU represents about 68,000 railroad employes, 27 percent of unionized rail employes. Included are 5,000 firemen, who work as assistants to engineers, and 3,000 hostlers, who move engines within yards.

The sticking point on the first agreement was railroading's hoariest labor issue: what to do with firemen. While in the days of steam power they shoveled coal into boilers, they have not been needed on locomotives for decades. Through a tortuous series of arbitrations, court proceedings and presidential panels, they have survived.

After the first tentative pact June 22, UTU committees representing brakemen, conductors, switchmen, engineers, dining-car stewards and yardmasters voted for ratification. The firemen's committee, which also represents the hostlers, voted against, 46 to 43.

That invalidated the entire agreement and began a complicated process dictated by the Railway Labor Act. Arbitration was offered, and the UTU declined.

The mediation board notified President Reagan that the dispute threatened to deprive sections of the country of essential transportation. He appointed a three-member emergency board, which reported Sept. 25, starting a 30-day cooling-off period now in progress.

The board's report praised the tentative agreement except for the section on firemen, which it said was too generous in permitting arbitration. " . . . Locomotive firemen should be eliminated without further delay subject to attrition and, where appropriate, other protective benefits," the board wrote.

National rail strikes tend to last just a few days, until Congress passes hurry-up legislation that usually imposes the board's solution. Facing those facts, the UTU and the railroads resumed negotiations Monday under Mediation Board Chairman Walter C. Wallace.

While nobody would say, it is assumed that the three days of negotiations dealt primarily with firemen, although the entire tentative contract technically had been reopened.