What year is your car?
It does sound like a problem with the central (or comfort) control unit. Beware though if you have a particular door(s) that lock sometimes and not others, the door lock units are also prone to problems on older B5's.
Mine was acting flaky (similar to above spontaneous locking) when it was new, control unit was replaced under warranty. But for the last year or so the left rear door sometimes doesn't lock like the rest. If you open it though, the alarm will go off. Cycling the lock knob up and down a few times will make it behave correctly again for a while.
For ref if you do have a bad door lock unit, someone posted once that it could be disassembled and repaired because the problem is usually just a broken solder or other electrical connection inside the unit. The hardest part is that you have to disassemble much of the door to get to it.