First of all, since you had a lease with the landlord, if he allowed you to move out, and then re-signed with the new tenant, he must follow CC 1950.5 and account for the security deposit under the old lease. You were on the lease, he should account to you. If he does not, he may have violated his obligations to you and could be liable for 2 times the amount that you paid. It sounds like he did not account to you.
I do not know if he accounted for the both of you to your ex-roommate. You may have to sue both of them in small claims court if it is less than $10,000.
Let them prove what they each did in court. You can consult with an attorney, and ask the court to reimburse you for this if you are the prevailing party.
This is general legal advice intended for informational purposes only and does not create and attorney/client relationship. If you wish further advice, please contact an attorney of your own choosing or you may contact me for further advice and make other arrangements including retaining my services.