The modeling of multiphase fluid mixture and its flow in porous media is of great interest in the field of reservoir simulation. In this paper, we formulate a novel energy-based framework to model multi-component two-phase fluid systems at equilibrium. Peng-Robinson equation of state (EOS) is used to model the bulk properties of each phase, though our framework works well also with other equations of state. Our model reduces to the conventional compositional grading if restricted to one spatial vertical dimension together with the assumption of monodisperse pore-size distribution (all pores being one size). However, our model can be combined with a general distribution of pore size, which can generate interesting behaviors of capillarity in porous media. In particular, the model can be used to predict the capillary pressure of two-phase fluid as a function of saturation, with a given pore-size distribution. This model is the quantitative study of the first time in the literature for the capillarity of a two-phase fluid with partial miscibility. We proposed an unconditional-stable energy-decay numerical algorithm based on convex-concave splitting, which has been demonstrated to be both robust and efficient using numerical examples. To verify our model, we simulate the compositional grading of a binary fluid mixture consisting of carbon dioxide and normal decane. To demonstrate powerful features of our model, we provide an interesting example of fluid mixture in a porous medium with wide pore size distribution, where the competition of capillarity and gravity is observed. This work represents the first effort in the literature that rigorously incorporates capillarity and gravity effects into EOS-based phase equilibrium modeling.