A pedestrian walks past Fannie Mae headquarters in Washington in this 2009 photo. (Joshua Roberts/Bloomberg)

A federal judge on Tuesday threw out a lawsuit brought by Fannie Mae and Freddie Mac investors to stop the government from seizing most of the profits at the mortgage finance twins.

The decision arrives more than a year after hedge fund Perry Capital sued the Treasury Department and the Federal Housing Finance Agency, which oversees the government-owned mortgage companies. The fund said they had violated a 2008 law that placed Fannie and Freddie into conservatorship to prevent bankruptcy.

Congress originally authorized Treasury to collect 10 percent dividend payments from Fannie and Freddie every quarter as a condition of the government’s $188 billion bailout of them. Treasury amended the terms of the agreement in 2012 to make Fannie and Freddie give the government most of their profits, a move known as the “sweep amendment.”

Perry Capital alleged that the dividend sweep was tantamount to a purchase of new securities, which Treasury did not have the authority to make. It also claimed the FHFA failed to conserve the assets of Fannie and Freddie by allowing Treasury to take most of their profits.

The hedge fund argued that Treasury’s arrangement caused irreparable harm to all private investors, saying they have been shortchanged as Fannie and Freddie returned to profitability. In its original complaint, the hedge fund said the government “maneuvered to ensure that Treasury would be the sole beneficiary of the companies’ improved financial position.”

In Tuesday’s ruling, U.S. Distinct Judge Royce C. Lamberth expressed sympathy for the investors, saying it was “understandable” for the sweep to “raise eyebrows or even engender a feeling of discomfort.” But “a sense of unease” over the government’s actions was not enough to reverse course.

Congress gave Treasury the power to take “unprecedented steps to salvage” the mortgage finance industry, so investors should take up their grievances with lawmakers, the judge said. He found that Treasury and FHFA were well within their rights, as dictated by Congress.

Treasury and FHFA declined to comment on the lawsuit. Perry Capital’s attorneys at Gibson, Dunn & Crutcher did not immediately respond to requests for comment.

Treasury has received $218.7 billion in dividend payments on the government’s nearly 80 percent stake in Fannie and Freddie. Shareholders have said Treasury’s terms prevent the companies from building capital that would help them redeem any of the shares the government has taken.

Perry Capital, which has not disclosed its exact stake in Fannie and Freddie, began investing in the mortgage companies in 2010. The hedge fund has said it believed the beleaguered companies were positioned to return to profitability. In the aftermath of the financial crisis, Fannie, Freddie and other government-backed agencies have insured nearly 90 percent of new mortgages.

Investors, including Perry Capital and Paulson & Co., have urged Congress to quash plans to close Fannie and Freddie, which they said should be allowed to go private. But the overwhelming momentum in Washington is behind abolishing the mortgage insurers.

In the spring, dozens of Fannie and Freddie shareholders, including consumer activist Ralph Nader, flocked to Capitol Hill to protest a Senate proposal to overhaul the mortgage finance twins.

Shareholders argued that the bill, which aims to replace the firms with a new entity and shift the risks of mortgage lending to the private sector, would trample on their rights. The legislation, sponsored by Sens. Tim Johnson (D-S.D.) and Mike Crapo (R-Idaho), failed to garner support from key Democrats and stalled in the chamber.

Dina ElBoghdady contributed to this report.