WASHINGTON—The federal government posted a budget surplus in December after mortgage giants Fannie Mae and Freddie Mac posted big payments to the Treasury, further narrowing the deficit in the first quarter of the fiscal year.

Revenues outpaced spending by $53.22 billion in December, the first surplus for the month since the 2007 fiscal year and the biggest on record. Economists surveyed by Dow Jones had forecast a $44.5 billion surplus.

...