If the bacteria were deep inside a rock ejected by an impact on a planet, which then falls as a meteorite on the other planet (as it is known to have happened for some meteorites fallen on Earth, which had been ejected from Mars or from Moon), then bacterial spores could survive, even if the external layers of the original rock are vaporized by the heat, if the rock had been big enough, so that the inner part would not have reached a high temperature.
The same happens with the astronauts coming back to Earth, who survive despite the external shield being vaporized by the heat. If a body has low thermal conductivity and high thermal capacity, the inner part will maintain a low enough temperature, even when the outer part is above boiling temperature.
On Earth, one can find bacteria in some rocks that are not completely compact, but which have pores or crevices, through which the water and gases needed by bacteria can pass.
Such events would be unlikely, but not impossible.
In the early Solar System, when big impacts were very frequent, there must have been many cases of rocks traveling between close planets, in all directions.
After the planets had reached their current structure, meteorites coming from either Mars or Moon to Earth have remained frequent, due to the lack of a thick atmosphere on Mars and Moon, combined with their weaker gravity, but meteorites going from Earth to any of them have become extremely unlikely, because most bodies colliding with the Earth do not reach its surface, and for the few that reach the surface the probability that an ejected rock could exit Earth's atmosphere is even lower.