What to do when asked to do something that seems unethical by a co-advisor which might result in losing credit for my work
You shouldn't start a fight with your coadvisor, but you shouldn't let them push you into an ethical compromise, either. (Or perhaps into a deeper ethical compromise — most instructors expect course projects to be the work of the students, not the students and their advisors.)

If you're not comfortable hiding details from your class/instructor, say so. Directly and professionally, without accusing him of shady behavior. Exactly as if your coadvisor were a responsible, ethical, adult human being.


  he refused to write me a recommendation letter to prevent me from leaving the university


This is where I would normally write "Walk away". Your coadvisor is using you. Fortunately, it looks like you've already figured that out.


  Moreover, I intend to leave the university for PhD somewhere else


Good.


  (he doesn't know that yet)


Not so good. Why are you keeping this a secret?