xdotool key Caps_Lock
Another alternative solution is to use xmacro:
printf 'KeyStrPress Caps_Lock\nKeyStrRelease Caps_Lock\n' | xmacroplay $DISPLAY
Basically, the reason is that when faking a Caps Lock key press programmatically in order for an application to toggle the Caps Lock state, the led does not toggle and the state becomes out of sync between the led and the keyboard.
It used to happen when entering a word like tHIS in OpenOffice with Caps Lock on, though late versions seem to have fixed it. But I've also suffered it when using x11vnc and pressing Caps Lock in the client. At that moment, the keyboard becomes desynced. The command above syncs it again. The source code in the bug report linked above works well too, in case you can't find xdotool or xmacro. They come as Debian and Ubuntu packages, though, so they should be easy to find.