FMRSA (meticillin-resistant Staphylococcus aureus) became resistant to penicillin by a change in its DD-transpeptidase. What triggered this change? Did it happen by chance or does the bacterium have some sort of response mechanism to cause this change on detecting penicillin?
The bacteria doesn't know.
When an organism copies its DNA, it sometimes makes mistakes. Different organisms have different rates of making mistakes; the rate of mistakes can be really high in some organisms like bacteria.
DNA copying mistakes (or unintended changes without copying, like due to UV damage) can sometimes be a big problem for multicellular organisms. If some of your cells have different DNA than others, they might function poorly, or they might grow too fast, stealing the space and nutrients from other cells in the organism, causing cancer. Multicellular organisms like humans have a lot of DNA repair mechanisms and methods to detect DNA damage and have cells sometimes suicide when they have been damaged.
For bacteria, DNA copying mistakes aren't as much of a problem, and there are lots and lots of bacteria out in the world. That means a lot of different mistakes. When a mistake is bad for a bacterium, it is more likely to die. Those types of mistakes therefore don't get recopied over and over again.
Sometimes a mistake is accidentally helpful.
Occasionally, a mistake actually makes a positive difference for an organism. If there are trillions of bacteria, just a few of them might have mistakes that make them resistant to a particular antibiotic. Let's say it's 1 out of 1,000,000,000.
In normal conditions, that doesn't matter much. If it doesn't hurt them at all, they will just keep on replicating the mistake, and other bacteria keep on replicating no mistake, and the proportion stays 1 out of 1,000,000,000.
Accidentally helpful mistakes become more common in a population
Now, let's say you expose that population to antibiotics that kill 99% of the bacteria with no mistake, but kills none of the bacteria with the mistake. Now, your 1,000,000,000 bacteria is 10,000,000 bacteria, but you still have 1 with a mistake. Now when those bacteria replicate, 1/10,000,000 will have the mistake. If they grow evenly back to 1,000,000,000 now you have 100/1,000,000,000 bacteria with the mistake. As you repeat this process, eventually you will have lots of bacteria that have the mistake, and that proportion of individuals will be insensitive to the antibiotic.
Biologists call these mistakes "mutations" and they call the increased survival and reproduction of organisms with helpful mutations "natural selection" - over time, natural selection is an important contributing factor to evolution of species.
At no point in this process is any awareness of what's happening necessary! All you need is variation, caused by replication mistakes, and some selection pressure that gives an advantage to individuals with certain mistakes. Making some mistakes in replication is a constant source of variation in biology, and the key process that, along with selection, has led to the vast variety of life around you.