to lock him in, make a pressure plate linked to a hatch/grate so he can go in, but not out. Have a magma tank above the room, the magma held up by a bridge or a series of hatches. Make a pressure plate that he paths over release magma into the room. All that is important is that enough magma drains in 100 steps (200 for bridge), to guarantee he dies. If he only crosses that pressure plate once, it won't jam or get out of sync. Put a drain in the room so it will clear (but not clear so fast that the dwarf survives. On the level with the magma tank, have a section that isn't directly over a hatch/bridge, and place a pressure plate there. When the magma on the plate is below 4/7 or 5/7 (whatever you've calculated to be necessary), it causes pumps to refill it, but this won't interfere with the plate locking the dwarf in when he tries to path out or the plate that ensures the roof is only open for 100 steps.
Only problem I can see is that the dwarf will try to path out once there is magma, and he will trigger the plate that opens the magma hatch, this could be a problem if you use bridges, so stick to a series of hatches. The other option is you can place the lock after the trigger. In that case you will want to use a bridge, but you have to construct the room in such a way that the initial rush of water won't break his pathing or cause him to flee before passing over the lock.
you can also do this with water, but its a bit trickier because you have to count on the dwarf getting pushed into an open space to his death, although then you can recover the body.