Device-to-device (D2D) communications enable new user experiences and low latency in communications among devices for new IoT applications, such as augmented reality (AR), virtual reality (VR), public safety, based on the fifth-generation and beyond (B5G) mobile networks. However, typical D2D communications still rely on the assistance of a centralized component, i.e., proximity service (ProSe) application server, for access control during device discovery procedures in mobile networks. Moreover, D2D communications are mainly launched by certain apps running on user equipment (UE) and need to discover the other UE in the same proximity of a base station (i.e., gNB in 5G) according to the identity or the profile of each UE in an app. This procedure will inevitably disclose the user/application’s sensitive information and behaviors to the infrastructures above to assist in establishing the corresponding D2D communications. Moreover, most of related works for secure D2D communication cannot support fine-grained access control and hidden policy during device discovery procedure simultaneously. Thus, this work proposes a new multi-access edge computing (MEC) based secure anonymous D2D communications system, so-called SAD2D, based on our newly proposed cooperative anonymous attributebased encryption (CoAABE). The security proofs of the proposed fundamental CoAABE scheme and the SAD2D protocol are both provably secure. Additionally, this work conducts the performance evaluation for the SAD2D in the aspect of queueing model, which can reflect the effects of device discovery requests in certain arrival rates regarding the performance. Overall, this work paves the way to achieve fine-grain access controllable security and privacy protection simultaneously for secure D2D communications to B5G MEC-enabled IoT applications.