The bill also grants the US government the power to continue with its warrantless surveillance scheme.
The Bush administration faced criticism when details emerged of its programme to monitor the phone calls of foreign targets in the US without warrants.
President Bush said the scheme was needed to prevent attacks on the US.
Telephone companies were facing as many as 40 lawsuits for their involvement in the scheme.
Sticking point
The bill was passed in the House of Representatives with bipartisan support, after House Democrats reached a compromise with the Republicans over the provisions to grant telecommunications companies immunity from prosecution.
Previous attempts to pass the bill - and allow the warrantless wiretap scheme to continue - had foundered on the issue of immunity for telecom firms.
Democrats had been reluctant to grant the firms immunity, saying the courts should first determine what the companies did.
President Bush - backed by Republicans in Congress - wanted to ensure that firms which had helped his administration would not be liable for prosecution.
The two sides reached a compromise, whereby telephone companies would not be automatically immune, but courts would be obliged to dismiss a suit against a firm if it is able to produce written certification that the White House asked it to participate in the program and assured the firm it was legal.
The bill is expected to pass the other house of Congress - the Senate - without any difficulties and will shortly become law.