The conventional electric power system is undergoing a transition from high fossil fuel dependence to significant renewable energy share primarily due to decreasing costs of renewable technologies, increasing environmental pollution, and favorable energy policies. The introduction of distributed energy resources with minimal power losses has also recently promoted the DC power systems deployment at small scale such as community-based DC microgrids. These systems allow the possibility of trading surplus energy from distributed energy resources with peer-to-peer (P2P) energy sharing. As power losses in a sharing scheme are non-linear concerning the distance between trading prosumers and power trade level, therefore, P2P energy sharing cannot be optimally managed with conventional factory-warehouse transportation techniques. In this work, we modeled a DC microgrid system with P2P sharing using a non-linear programming technique which allows the users to share their surplus energy from distributed energy resources with minimal system losses including distribution losses as well as conversion losses in comparison to conventionally employed factory-warehouse transportation technique. The proposed model is applied to a community microgrid having independent photovoltaic (PV) and battery systems installed at each house. Results show that the total system losses are reduced by up to 25% with the proposed optimization framework as compared to conventional factory-warehouse transportation sharing mechanism.