When you attack you'll notice there's two phases of the animation, the pull-back and the lunge. You need to tap the mouse button to attack as they begin their lunge. It should look like you're pulling back your musket and "catching" their stab, and then you'll immediately stab back while they're temporarily stunned. It's possible though for them to chamber your chamber-attack, so be careful. Two players standing still can play ping-pong with chambers for hours that way.
A good way to practice is with artillery ramrods, since they're so slow.