The regulator found large companies hold at least £202m from around 3.5m former domestic customers and £204m from 300,000 business accounts, saying it "expects suppliers to do more" to return the money.
It follows recent commitments by most major suppliers to automatically refund surpluses to current direct debit customers.
Ofgem said it had been inquiring into the balances held by energy suppliers, and into the companies' policies and practices, concerning customers who had closed accounts.
It found "an unacceptably large amount of money being retained rather than returned to consumers" and a wide variation in company practices.
The regulator said consumers needed to be confident they would not lose out if they closed an account with their present supplier.
It expected suppliers to do all they could to return money to individual consumers, and to tell consumers clearly what to do when closing an account.
Where it was not possible to repay a balance, it said suppliers should find ways to use the money to benefit consumers more widely.
A spokesman for Energy UK, the trade association for the energy industry, said energy companies tried to ensure customers' money got back to them, but that more commonly energy companies were owed many times more money by people who left unpaid debts behind them.