Deadlocks occur in operating systems when processes are blocked waiting for resources held by other blocked processes, forming a circular wait. There are four conditions for deadlock: mutual exclusion, hold and wait, no preemption, and circular wait. Deadlocks can be modeled using a resource allocation graph (RAG) with processes and resources as nodes and request/assignment edges. A cycle in the RAG indicates a deadlock. Detection algorithms work by maintaining a wait-for graph (WFG) and periodically searching for cycles, while avoidance methods analyze resource requests to allow only safe sequences.