Merry travelled with the company to Parth Galen, where he and Pippin were captured by the orcs. He didn’t surrender and managed to escape from Sauron’s servants.
https://lord-of-the-rings.org/books/meriadoc.html
Were it not for a wrangle among the orcs and an order not to kill the captives, the escape would never have taken place. But luck was on Pippin and Merry’s side.