A numerical investigation of free convection flow and heat transfer of Al2O3-water nanofluid contained within a square cavity with partially heated, also cooled vertical walls influenced by a magnetic field has been conducted in this study. The cavity’s top and bottom horizontal walls are conceived to be adiabatic; furthermore, a heat-conducting triangular cylinder is positioned in the cavity's middle. Solving the dimensionless governing equations is done by adopting the Galerkin weighted residual method of finite element formulation. The impacts of leading parameters including Rayleigh number (103 ≤ Ra ≤ 106), Hartmann number (0 ≤ Ha ≤ 100), and solid-volume fraction of nanoparticles (0% ≤ ϕ ≤ 5%) on the velocity as well as temperature field are studied. Results are illustrated with regard to streamlines, isotherms, heat flux, and the average Nusselt number inside the cavity for the mentioned parameter. Outcomes demonstrated that affixing the nanoparticle volume fraction significantly diminishes the fluid velocity but augments the heat transfer. For the concentrations of 1%, 3%, and 5%, respectively, it is roughly 2.17%, 6.51%, and 11.01% higher than base fluid water. In addition, the flow field is also found to be remarkably changing with a higher Rayleigh number. More discretely, the average Nusselt number enhances as the nanoparticle volume fraction and the Rayleigh number intensify, whereas with a higher Hartmann number, the opposite tendency is exhibited. For rising Rayleigh numbers, there has been a drop in heat transfer of 4.54% at Ha = 20, and of 12.56% and 23.28% at Ha = 50 and 100 in comparison to Ha = 0.