The Internet of Things (IoT) has brought about significant transformations in multiple sectors, including healthcare and navigation systems, by offering essential functionalities crucial for their operations. Nevertheless, there is ongoing debate surrounding the unexplored possibilities of the IoT within the energy industry. The requirement to better the performance of distributed energy systems necessitates transitioning from traditional mission-critical electric smart grid systems to digital twin-based IoT frameworks. Energy storage systems (ESSs) used within nano-grids have the potential to enhance energy utilization, fortify resilience, and promote sustainable practices by effectively storing surplus energy. The present study introduces a conceptual framework consisting of two fundamental modules: (1) Power optimization of energy storage systems (ESSs) in peer-to-peer (P2P) energy trading. (2) Task orchestration in IoT-enabled environments using digital twin technology. The optimization of energy storage systems (ESSs) aims to effectively manage surplus ESS energy by employing particle swarm optimization (PSO) techniques. This approach is designed to fulfill the energy needs of the ESS itself as well as meet the specific requirements of participating nano-grids. The primary objective of the IoT task orchestration system, which is based on the concept of digital twins, is to enhance the process of peer-to-peer nano-grid energy trading. This is achieved by integrating virtual control mechanisms through orchestration technology combining task generation, device virtualization, task mapping, task scheduling, and task allocation and deployment. The nano-grid energy trading system's architecture utilizes IoT sensors and Raspberry Pi-based edge technology to enable virtual operation. The evaluation of the proposed study is carried out through the examination of a simulated dataset derived from nano-grid dwellings. This research analyzes the efficacy of optimization approaches in mitigating energy trading costs and optimizing power utilization in energy storage systems (ESSs). The coordination of IoT devices is crucial in improving the system's overall efficiency.