The Office for National Statistics (ONS) said the state had to borrow a net £488 million - the first time in three years there was a deficit rather than a surplus in July.
The public coffers usually show a surplus in July due to big tax payments by companies and individuals, but these were outstripped by an increase in spending by central government departments.
July's deficit was much worse than economists' average prediction of a £2.5 billion surplus, and they said the figures suggest Britain's improving economy has yet to boost the state's finances.
But the Treasury insisted the economy is moving from "rescue to recovery" after the ONS reported higher tax receipts.
The ONS said public sector net borrowing, stripping out distortions from bank bailouts and quantitative easing (QE) cash, swung £1.3 billion into the red in July from an £823 million surplus a year earlier.
The figures also showed underlying public sector net debt as a proportion of the UK's gross domestic product (GDP) or total output hit a record for July at 74.5%.
Once a transfer of around £400 million of QE cash was included, public sector net borrowing was £62 million in July - the Government's preferred measure. But this was still £885 million higher than the £823 million surplus a year earlier.
The ONS said higher central government spending was spread across departments, adding that the Treasury expects this to be revised lower in coming months.
A Treasury spokeswoman said: "Strong tax receipts in July confirm that the economy is moving from rescue to recovery.
"There is still a long way to go as the UK recovers from the biggest economic crisis in living memory, and the Government is sticking to the economic plan that has already cut the deficit by a third and enabled the private sector to create over 1.3 million new jobs."
The ONS figures showed total tax receipts excluding QE cash were 3.4% higher year on year at £54.1 billion in July, helped by increases in VAT sales tax, income tax, National Insurance contributions and stamp duty on home purchases.
But corporation tax receipts dipped to £7 billion from £7.1 billion a year earlier, despite increasing signs of growth in the economy.
Central government spending rose by 4% to £51.2 billion, which the Treasury said was inflated by transfers to the Department of Health, Department for Education and overseas aid.
Total public sector net debt, stripping out the temporary cost of bank bail-outs, stood at £1.19 trillion in July, up 7.3% from £1.11 trillion a year earlier.
Chris Leslie, shadow financial secretary to the Treasury, said: "Another month of disappointing figures raises very serious concerns that borrowing continues to be way off track.
"Instead of the expected surplus in what is usually a good month for the public finances, Government borrowing increased, with public finances £1.3 billion worse this July than last.
"Because of his continued complacency, George Osborne is failing to get the deficit down because he has failed to boost living standards and we have seen three years of almost flat economic growth.
"He's borrowing billions more than planned simply to pay for the costs of his economic failure. And his promise to balance the books by 2015 is now in tatters."
The independent Office for Budget Responsibility (OBR), set up to monitor public finances, expects the deficit to come in at around £120 billion for the year to the end of next March, above last year's £116.5 billion deficit.
JP Morgan economist Allan Monks said despite July's unexpected deficit, public sector borrowing should still undershoot the OBR's target.
He said: "The upswing in growth at present adds some risk that fiscal year borrowing will come in even lower than this."
Economists at Investec Securities added that there should be a "moderate" improvement in the public finances this year, as the recovery gains traction and the housing market strengthens.
Figures showed the economy expanded by 0.6% in the second quarter, from 0.3% growth in the first three months of the year. Economists increasingly believe the UK is on course to match or beat second-quarter growth in the July to September period.