Change bagging to begging
Both would be grammatically correct with a small difference in context
"He is begging me to pet him"
You, just yourself are being asked to pet him
"He is begging to be petted"
He wants to be petted by anyone.
"Bagging" is used for examples as "He is bagging (packing) my groceries" or to place items into a bag.
What exactly is the context? Are you talking about your dog? Do you mean begging instead of bagging? Give us a few more clues, please.
If you mean your dog, and the sentences are supposed to be like this:
He is begging me to pet him.
He is begging to be petted.
then they are both fine sentences. The first one means that the dog wants you to pet him. The second one means that the dog doesn't care who pets him, as long as somebody does.