For a debit to a closed account, the answer is "It depends." If the merchant failed to obtain a valid authorization or submitted the transaction late after obtaining a valid authorization, you may file a chargeback citing the lack of authorization to recover the funds. If the merchant obtained a valid authorization and the customer closed the account before the transaction posted, then you will have to seek reimbursement from your customer.
For credits, many merchants will only issue refunds to the card that made the initial purchase. Since your bank pays a fee to reject the transaction, it's not worth the time and expense. In this case, you can either hold the funds in an internal account until they have to be escheated according to state law, mail a cashier's check to the customer's last known address (and escheat it to the state if they never cash it), or try contacting the customer.