Deadlock occurs when a set of processes are blocked waiting for resources held by each other in a cyclic manner. There are four necessary conditions for deadlock: mutual exclusion, hold and wait, no preemption, and circular wait. Deadlock can be prevented by ensuring that at least one condition is never satisfied through methods like resource ordering or avoidance by tracking resource allocation to guarantee safe states. Detection identifies when deadlock has occurred, while recovery requires aborting processes or preempting resources to break cycles.