Map CapsLock key to ESC in Mac OS X
My friend pointed me to something that worked. He said to install both PCKeyboardHack and KeyRemap4MacBook. PCKeyboardHack allows for remapping capslock, while the other one seems to allow for a lot more remapping. I'm not sure if both are actually required (you might be able to use just PCKeyboardHack), but I installed both just to play with them. Seems to be working well for me.
Since macOS 10.12.1 it is possible to remap Caps Lock to Esc natively (System Preferences -> Keyboard -> Modifier Keys).