I press modifier on keyboard A, keyboard A sends key press event to the OS. I press key on keyboard B, keyboard B sends key press event to the OS. I release key on keyboard B, keyboard B sends key release event to the OS. I release modifier on keyboard A, keyboard A sends key release event to the OS.
From the sequence of events, the OS knows that a key was pressed with the modifier and can act accordingly e.g. emit an event downstream that corresponds to the key that should be generated when pressing a modifier.
Scanning the keyboard matrix is no concern for the OS.