In order to mitigate the intermittent nature of renewable energy sources, natural gas networks play a prominent role in integrating electricity and gas grids. Furthermore, energy storage systems, such as batteries and hydrogen, are essential for power balancing and managing energy. The interconnected operation of electricity and natural gas systems has previously been analyzed in terms of the operational constraints of their respective grids. A few studies have explored market-driven models like the peer-to-peer (P2P) energy transacting market for the interconnected optimization of electricity and gas grids. Only a few studies have been conducted on the P2P market in the context of gas and power systems, organized into two separate stages: trading and scheduling. First, customers schedule their energy based on wholesale energy prices. After the scheduling stage and determining their energy consumption and production for each hour of the next day, they initialize P2P energy trading through various decentralized methods, such as the continuous double auction (CDA) mechanism. This paper proposes an integrated P2P energy scheduling-based optimization for energy management of nano grids (NGs), gas-fired generators, and power-to-gas (P2G) units considering AC constraints of the electricity distribution grid, electricity network usage price, natural gas steady-state model, and demand response program. The word "integrated optimization" implies that the proposed method clears customers' energy scheduling based on the market competitive price. According to simulation results, the proposed method decreases total operating costs, reduces power loss, enhances synergy between network components, and guarantees gas and power grid performance.