The increase compared with a loss of $4.4 billion for the same quarter of 2011. It also marked the sixth quarter in which Freddie sought no additional aid since being taken over by the government in September 2008.

The gain ‘‘was driven by favorable market conditions, including the continued improvement in the housing market,’’ Freddie chief executive Donald Layton said.

Get Talking Points in your inbox:

An afternoon recap of the day’s most important business news, delivered weekdays.

The government rescued Freddie and larger sibling Fannie Mae during the 2008 financial crisis after both incurred massive losses on risky mortgages. Taxpayers have spent about $170 billion to rescue them, the costliest bailout of the 2008 financial crisis.

Under a new federal policy announced last summer, Fannie and Freddie have to turn over all profits they earn every quarter to the government. The change was made to ensure the companies pay the government back.

Fannie and Freddie are required to pay 10 percent dividends on the government money they receive.