Is it that the town is part of a different realm than the county itself?
Yep, and my guess is that this behavior is not intentional, but rather a side effect of the fact that you are made temporarily hostile to the holder of a county your raiders land in, but not hostile to everyone. That flag doesn't trigger on the second holding (and in most cases it doesn't matter because the second is the same as the first) and so it gets skipped and you pillage the next holding.
Try this: Next time, step into Amalfi first, you'll certainly start raiding them (Republics definitely are NOT immune). Step back to this place and siege down the first castle. Are you now attacking the city?
So what would happen to a hypothetical duchy if it swore allegiance to say the Byzantine Empire? Would it become a republic with all that entails? Or would little change?
Why would it become a Republic? Republics aren't defined by who you are in vassalage to. They're defined by being a Duke-or-higher mayor with a coastal capital. A hypothetical duchy that swore to the Byzantines would become just that, a duchy under the Byzantine Empire.