How could such a device as a mimicry appear at a stick, whose shape resembles a twig?

1. First, mutations appeared and phenotypically manifested, which turned out to be useful in certain environmental conditions.
2. Insects possessing this trait have gained an advantage in the struggle for existence.
3. Natural selection retained individuals with a useful trait, and the trait was fixed in generations

Remember: The process of learning a person lasts a lifetime. The value of the same knowledge for different people may be different, it is determined by their individual characteristics and needs. Therefore, knowledge is always needed at any age and position.