Those two things are not in any way mutually exclusive. Understanding the logic is an effective way to accurately predict the next token.
> I am fairly certain variations of this are in the training set.
Yes, which is probably how ChatGPT learned that logical principle. It has now learned to correctly apply that logical principle to novel situations. I suspect that this is very similar to how human beings learn logic as well.