Freddie Mac, the second largest provider of U.S. home mortgage funding, on Friday posted its first quarterly profit in two years as gains from hedges and a one-time accounting change offset still-lofty credit losses.

For the first quarter in four, Freddie Mac said it would not need a capital injection from the Treasury to maintain its business of providing credit for U.S. housing. But it continues to rely on the government for survival, it said.

Freddie Mac and larger rival Fannie Mae are seen as key barometers of the U.S. housing market, having expanded their scope as competitors fell during the financial crisis. Under government control since September, they are also being asked to do more for U.S. efforts to stabilize the shaky housing market, even though that is turning out to be a costly effort.

Together, the companies own or guarantee more than $5 trillion in U.S. mortgages.

Our outlook remains cautious due to rising foreclosures, growing unemployment, tight lending standards, and buyers' reluctance to reenter the market, John Koskinen, Freddie Mac's interim chief executive officer, said in a statement.

Freddie Mac reported second-quarter net income of $768 million, compared with a $9.9 billion loss in the first quarter and a $821 million deficit in the period a year ago.

After payments of $1.1 billion in preferred stock dividends to the U.S. Treasury, Freddie Mac had a net loss of 11 cents per diluted common share.

Freddie Mac said its net worth of $8.2 billion was cushioned by a $5.1 billion increase in equity due to the adoption of accounting rules that govern how it must recognize losses. It also had a $4.2 billion gain from derivatives as interest rates rose, and greater interest income as its borrowing costs fell.

Provisions for credit losses declined to $5.2 billion in the second quarter from $8.8 billion in the previous period, driven by recent home price improvements, it said. But that benefit is likely seasonal, and provisions will probably rise again, it said.

Fannie Mae on Thursday reported a $14.8 billion quarterly net loss, and requested $10.7 billion from Treasury under the senior preferred stock program.

Dire assessments from the two mortgage finance giants come as reports show signs of nascent recovery in housing, including rising home sales and the first monthly increase in home prices since 2006, according to one index. The companies both expect home prices to fall further.

The companies are reeling under the weight of dividends they must pay to the Treasury under their capital injections, which through last quarter totaled $45.9 billion for Fannie Mae and $51.7 billion for Freddie Mac. Annual dividends owned the Treasury by Freddie Mac are $5.2 billion.

Credit expenses are also likely to rise as Freddie Mac completes modifications of loans started under President Barack Obama's Making Home Affordable program, which aims to refinance or modify loans for as many as nine million Americans. To complete a modification, the company must purchase the loan out of the mortgage-backed securities, and recognize a loss.

About 16,000 loans entered in the second quarter into a three-month trial period that precedes a modification, it said. Freddie Mac expects increasing numbers of eligible loans to enter into trial periods through December.

Analysts expect Freddie Mac and Fannie Mae to be subject to government control until Congress decides on how to reform the companies in a way that will provide strong support for housing while protecting taxpayers from future losses. The Obama administration said it would propose plans in February.

(Editing by Leslie Adler)