Government-owned mortgage giant Fannie Mae posted its most profitable year ever in 2012, earning $17.2 billion.

It's a major turnaround for the company, which reported a $16.9 billion loss in 2011 and hadn't seen a full-year profit since 2006. But with the housing market enjoying a resurgence, the company expects to remain profitable going forward. And, in a welcome development for taxpayers, that means Fannie Mae can start paying back the billions of dollars it's received since the government took it over at the height of the financial crisis.

"Solid business fundamentals such as improving performance of our book of business and improvements in the housing market led us to report the largest annual and quarterly net income in the company’s history," Susan McFarland, Fannie's executive vice president and chief financial officer, said in a press release. "We expect to remain profitable for the foreseeable future and return significant value to taxpayers."

The government stepped in to rescue Fannie Mae and sister firm Freddie Mac in 2008, pumping billions of dollars into the two companies in what quickly became one of the most contentious moves of the federal bailout. Fannie was able to return $11.6 billion to the federal government last year, but is still on the hook for an eye-popping $80.4 billion.

It was the first time since the government takeover that Fannie was able to pay back the government without needing to pull additional funds from the Treasury Department. Under the terms of the takeover, Fannie is required to pay quarterly dividends to the government, but at the same time is allowed to draw billions from the Treasury to shore up its finances. In 2011, for example, Fannie paid $9.6 billion to the government, but withdrew $25.6 billion.

Through mid-December, the firm had received about $116 billion in bailout money.

A settlement with Bank of America bolstered Fannie's profits last year, but the major driver of the company's turnaround has been the housing market's recovery. Fannie has now posted positive net earnings for the past five quarters.

From the Wall Street Journal's Nick Timiraos: 

In addition to rising home prices, Fannie and Freddie are also seeing their fortunes rise as a result of much safer loans that they have bought or guaranteed over the past four years. At Fannie, the rate of serious delinquencies on single-family loans — those at least three months past due or awaiting foreclosure — declined to 3.29 percent, compared with 3.91 percent a year earlier. The rate has declined each quarter since the first quarter of 2010. [Wall Street Journal]

Fannie could soon pay back even more money to the government when it reports its first-quarter earnings next month. As Bloomberg notes, the company could determine it is now financially healthy enough to reverse billions in past write-downs, which would allow it to send an additional $58 billion to the Treasury.

Still, the company has a long way to go to reimburse taxpayers. And what Fannie's sudden profitability means for the company's future is unclear. The government has said it wants to reduce its role in the housing market and potentially sell off the mortgage giant as part of a broader housing finance industry overhaul. But Fannie's newfound profitability may put a damper on Washington's willingness to act.

From CNBC's Eamon Javers: 

Lawmakers could see an era of steady profits from Fannie Mae as a signal that the crisis is over, easing pressure to decide whether or not to unwind the government's significant involvement in the nation's housing finance. [CNBC]

Plus, profits from Fannie Mae could be seen as a rare piece of budgetary good news in a capital that's otherwise drowning in debt, which could provide more impetus to freeze the current post-bailout situation in place.