In this paper, a cognitive machine-to-machine (M2M) communication network is considered, in which a cellular network shares the spectrum with the M2M communication network with M machine-type devices (MTDs), one half-duplex relay, and one MTD gateway for data gathering. One key challenge is that in the future 5G wireless networks, there will be billions of those small MTDs, and therefore, a MTD selection protocol is required for managing data transmission between MTDs. A joint buffer-aided MTD selection and power allocation protocol is proposed to maximize the MTDs' sum-rate provided that the induced interference to the cellular network is limited. In particular, in the proposed scheme, at each time slot and each subcarrier, the cognitive M2M network optimally decides on whether to be silent or to select either the relay or one of the MTDs for data transmission. To this end, for each MTD, there exists a buffer at the relay to avoid data loss. The closed-form expressions for the power coefficients of MTDs are calculated. Simulation results show that the proposed policy improves the sum-rate of the MTDs in comparison with the other proposed schemes for M2M communication without buffer.