The House last night passed, 337 to 20, a compromise rail deregulation bill that would pare nearly a century of government regulation of the nation's railroads.

The bill passed handily after the House adopted, by an overwhelming vote of 369 to 6, compromise language that sought to strike a balance between greater rate-setting freedom for the railroads and protection for "captive" shippers who feared they would be gouged by high rates on monopoly railroads.

President Carter last night hailed passage of the measure, saying it would help renew the failing rail freight industry. "By eliminating needless, burdensome rules, it will allow railroads to provide improved service, and it will help restore the industry's financial health," he said.

Sponsored by Commerce Committee Chairman Harley O. Staggers (D-W.Va.) and Reps. Nick J. Rahall (D-W.Va.) and Gary A. Lee (R-N.Y.), the compromise would phase in rate-setting freedom of the railroads over the next four years while preserving the ability of the Interstate Commerce Commission to step in and roll back freight rate increases it finds unreasonably high.

Passage of the compromise followed the sound defeat, 296 to 83, of a substitute by Rep. Bob Eckhardt (D-Tex.) that would have limited more sharply the railroads' ability to set freight rates without government interference.

The rail bill is the last of three transportation deregulation measures supported by the Carter administration and considered by Congress. An airline deregulation act, the most extensive of the three, was signed into law in October 1978, and a trucking measure was signed by the president in July.

The bills were designed to introduce more competition into traditionally heavily regulated sectors of the economy.

Heavy federal regulation is blamed in part for the lackluster performance of the railroads in recent years. Although some are profitable, even very profitable, in general they make no more than an average of 2 percent rate of return on investment, far less than needed to provide efficient and reliable service.

"Currently, railroads are required to carry shipments at a loss," Rep. James J. Florio (D-N.J.), a principal sponsor of the measure, said during debate yesterday. "If that continues, we have two choices: the railroads will go under, or the railroads and their supporters will come to Congress, as they do periodically, and ask for money.

"I don't think either of those choices are acceptable to the American people." he said.

Florio, chairman of the House transportation and commerce subcommittee, and the bill's chief cosponsor, Edward Madigan (R-Ill.), the subcommittee's ranking minority member, had pronounced the bill dead two weeks ago, but agree, after pleas from the administration, to try again.

In the last two weeks, the administration pulled out all the stops in seeking support for the compromise that Florio and Madigan promoted. Each member of the House got letters from the secretaries of energy, transportation and agriculture, and more than 100 calls were made from the White House, at least a half a dozen from President Carter.

The bill must go to a conference with the Senate, which passed a rail deregulation measure in April.

"The reforms contained on this measure and its Senate counterpart will give the railroads more freedom than they have ever had to price their services according to market demands instead of on the basis of regulatory dictates," ICC Chairman Darius W. Gaskins Jr. said last night.

In the compromise adopted yesterday the ICC would retain jurisdiction if a railroad increased its rates to a level more than 160 percent of its variable costs -- the out-of-pocket costs of actually transporting goods.

The level is designed to allow a railroad to cover its variable costs and to make a contribution to its fixed costs, such as roadbed maintenance, but does not include any profit. The second year, the ICC threshhold would become 165 percent; the third year, 170 percent, and the fourth year, 175 percent.

After that, ICC involvement would be triggered by a complicated cost-recovery formula.

ICC involvement would not, however, mean that the agency necessarily would disallow a higher rate.The ICC could find higher rates permissible, particularly when profits are figured in.