No, the GPL doesn't apply automatically like that. But the result of using GPL-covered code that you thought wasn't GPL-covered is that you accidentally committed copyright infringement. There are various ways to stop this infringement, such as (retroactively) offering your software under the GPL as well, or by stopping to use the GPL-covered software. But this doesn't change what has already happened. For the GPLv3, if you fix any infringment in a certain time frame after being notified of the problem, the license is automatically reinstated. This limits your risks for accidental infringement.
Depending on where you are, open source licenses like the GPL can be understood as contracts. A defining feature of contracts is the meeting of minds: all parties to the contract agree on the same thing. And contracts have to be accepted by all parties in order to have effect. This means you can't be tricked by imposing terms that you haven't accepted, e.g.:
- A: If you say “what” you owe me $100.
- B: What??
- A: ok, pay up!
Similarly, you can't be forced to open-source your code because you accidentally used GPL-covered code. But nothing except the GPL gives you the right to copy or modify the software.
A different view is that licenses like the GPL are unilateral promises by the copyright holder not to sue you if you comply with the license terms. You haven't complied with the license so you can be sued successfully. But what would a court award for this infringment (or what would be negotiated in a settlement)? They don't automatically get a right to decide what you should do with your code.
The best legal problem is that which never arises. It's therefore sensible to manage licensing risks, for example by auditing licensing status and contribution policies of the dependency graph of your software. If you buy commercial software you likely receive warranties that the seller holds the necessary rights to the software. If that isn't the case, you might be able to sue them for the resulting costs.