The return of security deposits is governed by state law. And unfortunately state law lacks any precision in addressing this issue. But it appears that the federal Protecting Tenants at Foreclosure Act decides the issue, as it provides that leases survive foreclosure. In California, this means that the lender, as successor in interest, does have to return your security deposit.
If the lender or new owner demands rent for the 90-day notice period, the lender or new owner must return your security deposit. However, if the lender or new owner doesn't require that you pay rent, you have recovered your security deposit in rent savings.
If you move before the Trustee Sale, you'd recover your rent from your landlord, just as you would if he weren't in foreclosure.