Romans 2: 12-16
Case Study
'For as many as have sinned without law shall also perish without law: and as many as have sinned in the law shall be judged by the law; (For not the hearers of the law are just before God, but the doers of the law shall be justified. For when the Gentiles, which have not the law, do by nature the things contained in the law, these, having not the law, are a law unto themselves: Which shew the work of the law written in their hearts, their conscience also bearing witness, and their thoughts the mean while accusing or else excusing one another;) In the day when God shall judge the secrets of men by Jesus Christ according to my gospel.'
The scripture is a case study. It examines Gentiles who don't have the law, but do the things contained in the law. How could a Gentile who has not the law, do by nature what the law requires? And ultimately how is he justified or put righteous, not knowing the law?
Let's get some definitions out of the way;
Apart from the law means apart from the law Moses brought down from Sinai written by the finger of God. Apart from the law does not mean ignorance of the law. There is a standard even for those who claim ignorance. Obeying the law does not necessarily make one righteous; but obeying the Spirit of the law in Christ does.
Under the law means living under the old system, not the new.
The phrase written in their heart, is an Old Testament promise found in Jeremiah 31:33, 'but this is the covenant that I will make with the house of Israel after those days, saith the LORD: I will put My law in their minds, and write it on their hearts.'
Christ takes upon himself our sins. His righteousness is imputed. The criteria for this exchange is faith in Him. It is those who obey the righteousness of the law written upon their hearts; who will be declared righteous before God.